Node.js on Google Cloud Platform

Try It Free View Documentation

Node.js tools to build cloud-native apps, deploy at scale, and analyze data

  • check Build planet-scale webapps
  • check Use Google’s machine learning APIs with native library support
  • check Write serverless apps with ease
  • check Create Big Data analysis applications without learning another language
lovingly-crafted npm modules

Take advantage of our APIs and services using the @google-cloud npm modules and test run our code samples.

Upload Files to Google Cloud Storage
Analyze Audio with Cloud Speech API
Writing to Stackdriver Logging with Winston
Using Google BigQuery
Upload Files to Google Cloud Storage
1
Install
npm install --save @google-cloud/storage
2
Set up a Cloud Platform Console project
  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Set up a Cloud Platform Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Cloud Storage API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the Cloud Platform Console.

3
Write your code
// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'Local file to upload, e.g. ./local/path/to/file.txt';

// Uploads a local file to the bucket
storage
  .bucket(bucketName)
  .upload(filename)
  .then(() => {
    console.log(`${filename} uploaded to ${bucketName}.`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });
Analyze Audio with Cloud Speech API
1
Install
npm install --save @google-cloud/speech
2
Set up a Cloud Platform Console project
  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Set up a Cloud Platform Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Cloud Speech API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the Cloud Platform Console.

3
Write your code
// Imports the Google Cloud client library
const fs = require('fs');
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';

const config = {
  encoding: encoding,
  sampleRateHertz: sampleRateHertz,
  languageCode: languageCode,
};
const audio = {
  content: fs.readFileSync(filename).toString('base64'),
};

const request = {
  config: config,
  audio: audio,
};

// Detects speech in the audio file
client
  .recognize(request)
  .then(data => {
    const response = data[0];
    const transcription = response.results
      .map(result => result.alternatives[0].transcript)
      .join('\n');
    console.log(`Transcription: `, transcription);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });
Writing to Cloud Logging with Winston
1
Install
npm install --save @google-cloud/logging-winston
2
Set up a Cloud Platform Console project
  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Set up a Cloud Platform Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Stackdriver Logging API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the Cloud Platform Console.

3
Write your code
const winston = require('winston');
const Logger = winston.Logger;
const Console = winston.transports.Console;

// Imports the Google Cloud client library for Winston
const LoggingWinston = require('@google-cloud/logging-winston');

// Creates a Winston Stackdriver Logging client
const loggingWinston = new LoggingWinston();

// Create a Winston logger that streams to Stackdriver Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/winston_log"
const logger = new Logger({
  level: 'info', // log at 'info' and above
  transports: [
    // Log to the console
    new Console(),
    // And log to Stackdriver Logging
    loggingWinston,
  ],
});

// Writes some log entries
logger.error('warp nacelles offline');
logger.info('shields at 99%');
Using Google BigQuery
1
Install
npm install --save @google-cloud/bigquery
2
Set up a Cloud Platform Console project
  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Set up a Cloud Platform Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the BigQuery API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the Cloud Platform Console.

3
Write your code
// Imports the Google Cloud client library
const BigQuery = require('@google-cloud/bigquery');

// The project ID to use, e.g. "your-project-id"
// const projectId = "your-project-id";

// The SQL query to run, e.g. "SELECT * FROM publicdata.samples.natality LIMIT 5;"
// const sqlQuery = "SELECT * FROM publicdata.samples.natality LIMIT 5;";

// Instantiates a client
const bigquery = BigQuery({
  projectId: projectId
});

// Query options list: https://cloud.google.com/bigquery/docs/reference/v2/jobs/query
const options = {
  query: sqlQuery,
  timeoutMs: 10000, // Time out after 10 seconds.
  useLegacySql: false // Use standard SQL syntax for queries.
};

// Runs the query
bigquery
  .query(options)
  .then((results) => {
    const rows = results[0];
    console.log('Rows:');
    rows.forEach((row) => console.log(row));
  })
  .catch((err) => {
    console.error('ERROR:', err);
  });
Node JS quickstarts
Quickly find and Debug issues

Google Stackdriver provides powerful monitoring, logging, and diagnostics. It equips you with insight into the health, performance, and availability of cloud-powered applications, enabling you to find and fix issues faster.

Google Stackdriver
Unified monitoring, logging, and diagnostics for applications on Google Cloud Platform and AWS.
Stackdriver Error Reporting
A walk through of getting an error alert and investigating the error in the Google Cloud Console.
Stackdriver Monitor, diagnose, and fix
In this video, Aja Hammerly uses Stackdriver to find and fix some subtle errors in an example app, and you'll learn how to use Stackdriver on your own projects.
Learn more
Join our community

Got something to say? Join our community to ask questions, or just chat with the experts at Google who help build Google Cloud Platform and Node.js.

Supervisa tus recursos estés donde estés

Obtén la app de Google Cloud Console para ayudarte a administrar tus proyectos.