Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.