Last active
August 26, 2019 01:33
-
-
Save jonbcampos/8c1455b17e60edb50060fbbab9d5f60d to your computer and use it in GitHub Desktop.
Main Application File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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