const express = require("express");
const app = express();
const port = 3000;
// use body-parser to automatically parse JSON-encoded request bodies
const bodyParser = require("body-parser");
app.use(bodyParser.json());
const helperMessage = { message: "Try posting JSON to this endpoint!" };
// add a helper message encouraging POSTing to this API
app.get("/", (req, res) => res.json(helperMessage));
// listen for POST requests and respond based on that POSTed data
app.post("/", (req, res) => {
if (req.body.key)
return res.send({ key: req.body.key });
return res.send({ message: `You posted ${JSON.stringify(req.body)}` });
});
app.listen(port, () => console.log(`Example API listening on port ${port}!`));
/*
Try it out by POSTing to this endpoint!
Get this API's URL from the Endpoint button at the top of the Notebook
curl -X "POST" <ENDPOINT URL> \
-H 'Content-Type: application/json; charset=utf-8' \
-d #x27;{ "key": "value" }'
*/