// Copyright 2019 Google LLC. All rights reserved. // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. // [START run_pubsub_server_setup] const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.json()); // [END run_pubsub_server_setup] // [START run_pubsub_handler] app.post('/', (req, res) => { if (!req.body) { const msg = 'no Pub/Sub message received'; console.error(`error: ${msg}`); res.status(400).send(`Bad Request: ${msg}`); return; } if (!req.body.message) { const msg = 'invalid Pub/Sub message format'; console.error(`error: ${msg}`); res.status(400).send(`Bad Request: ${msg}`); return; } const pubSubMessage = req.body.message; const name = pubSubMessage.data ? Buffer.from(pubSubMessage.data, 'base64').toString().trim() : 'World'; console.log(`Hello ${name}!`); res.status(204).send(); }); // [END run_pubsub_handler] module.exports = app;