Skip to content

Instantly share code, notes, and snippets.

@jonbcampos
Last active August 26, 2019 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbcampos/8c1455b17e60edb50060fbbab9d5f60d to your computer and use it in GitHub Desktop.
Save jonbcampos/8c1455b17e60edb50060fbbab9d5f60d to your computer and use it in GitHub Desktop.
Main Application File
// imports
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
// setup
app.use(bodyParser.json());
// POST endpoint
app.post("/", (req, res) => {
// parse body
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
}
// parse message
const pubSubMessage = req.body.message;
const originatorMessage = Buffer.from(pubSubMessage.data, "base64").toString().trim();
const now = new Date();
// log output
console.log(`${originatorMessage} says the time is ${now.toString()}`);
res.status(204).send()
});
// start server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => console.log(`listening on port ${PORT}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment