Node.js di Google Cloud Platform

Coba Gratis Lihat Dokumentasi

Fitur Node.js untuk membuat aplikasi berbasis cloud, men-deploy dalam skala luas, dan menganalisis data

  • Buat aplikasi web berskala raksasa
  • Gunakan API machine learning Google dengan dukungan library native
  • Tulis aplikasi tanpa server dengan mudah
  • Buat aplikasi analisis Big Data tanpa mempelajari bahasa lain
modul npm yang dibuat dengan penuh cinta

Manfaatkan API dan layanan kami menggunakan modul npm @google-cloud dan uji coba sampel kode kami.

Upload File ke Google Cloud Storage
Analisis Audio dengan Cloud Speech API
Menulis ke Stackdriver Logging dengan Winston
Menggunakan Google BigQuery
Upload File ke Google Cloud Storage
1
Instal
npm install --save @google-cloud/storage
2
Siapkan project Cloud Platform Console
  1. Sign in to your Google Account.

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

  2. Set up a GCP 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 GCP Console.

4
Tuliskan kode Anda
// 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
await storage.bucket(bucketName).upload(filename, {
  // Support for HTTP requests made with `Accept-Encoding: gzip`
  gzip: true,
  // By setting the option `destination`, you can change the name of the
  // object you are uploading to a bucket.
  metadata: {
    // Enable long-lived HTTP caching headers
    // Use only if the contents of the file will never change
    // (If the contents will change, use cacheControl: 'no-cache')
    cacheControl: 'public, max-age=31536000',
  },
});

console.log(`${filename} uploaded to ${bucketName}.`);
Analisis Audio dengan Cloud Speech API
1
Instal
npm install --save @google-cloud/speech
2
Siapkan project Cloud Platform Console
  1. Sign in to your Google Account.

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

  2. Set up a GCP 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 GCP Console.

3
Tuliskan kode Anda
// 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
const [response] = await client.recognize(request);
const transcription = response.results
  .map(result => result.alternatives[0].transcript)
  .join('\n');
console.log(`Transcription: `, transcription);
Menulis ke Cloud Logging dengan Winston
1
Instal
npm install --save @google-cloud/logging-winston
2
Siapkan project Cloud Platform Console
  1. Sign in to your Google Account.

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

  2. Set up a GCP 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 GCP Console.

3
Tuliskan kode Anda
const winston = require('winston');

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

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 = winston.createLogger({
  level: 'info',
  transports: [
    new winston.transports.Console(),
    // Add Stackdriver Logging
    loggingWinston,
  ],
});

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

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

  2. Set up a GCP 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 GCP Console.

3
Tuliskan kode Anda
// Import the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');

async function queryStackOverflow() {
  // Queries a public Stack Overflow dataset.

  // Create a client
  const bigqueryClient = new BigQuery();

  // The SQL query to run
  const sqlQuery = `SELECT
    CONCAT(
      'https://stackoverflow.com/questions/',
      CAST(id as STRING)) as url,
    view_count
    FROM \`bigquery-public-data.stackoverflow.posts_questions\`
    WHERE tags like '%google-bigquery%'
    ORDER BY view_count DESC
    LIMIT 10`;

  const options = {
    query: sqlQuery,
    // Location must match that of the dataset(s) referenced in the query.
    location: 'US',
  };

  // Run the query
  const [rows] = await bigqueryClient.query(options);

  console.log('Query Results:');
  rows.forEach(row => {
    const url = row['url'];
    const viewCount = row['view_count'];
    console.log(`url: ${url}, ${viewCount} views`);
  });
}
queryStackOverflow();
Panduan memulai Node JS
Mencari dan Mendebug masalah dengan cepat

Google Stackdriver menghadirkan pemantauan, logging, dan diagnosis yang andal. Stackdriver memberikan laporan kesehatan, performa, dan ketersediaan aplikasi yang didukung cloud sehingga Anda dapat lebih cepat menemukan dan memperbaiki masalah.

Google Stackdriver
Pemantauan, logging, dan diagnosis terpadu untuk aplikasi di Google Cloud Platform dan AWS.
Stackdriver Error Reporting
Panduan menghadapi peringatan error dan menginvestigasi error di Google Cloud Console.
Stackdriver Memantau, mendiagnosis, dan memperbaiki
Dalam video ini, Aja Hammerly menggunakan Stackdriver untuk menemukan dan memperbaiki beberapa error yang samar dalam aplikasi contoh, dan Anda akan mengetahui cara menggunakan Stackdriver di project Anda sendiri.
Pelajari lebih lanjut
Bergabunglah dengan komunitas kami

Ingin menyampaikan sesuatu? Bergabunglah dengan komunitas kami untuk bertanya, atau sekadar mengobrol dengan para pakar di Google yang membantu membangun Google Cloud Platform dan Node.js.