I have been working on implementing a JSON content listener in Typescript using Express and Body-parser. Everything is functioning perfectly, except when I receive the JSON webhook, it is logged as an object instead of a string.
import express from 'express';
import bodyParser from 'body-parser';
// Initializing Express and defining a port
const app = express();
const PORT = 3000;
// Starting Express on the defined port
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
// Basic error handling
app.use((err, req, res, next) => {
console.log(err.stack);
console.log(err.name);
console.log(err.code);
res.status(500).json({ message: 'Something went wrong' });
})
// Parsing JSON
app.use(bodyParser.json());
// Creating a route (URL set as webhook URL)
app.post('/webhook', (req, res, next) => {
console.log("Headers: " + req.headers.toString());
console.log("Body: " + req.body.toString());
next()
res.status(200).end();
});
The following is returned in the console:
Server running on port 3000
Headers: [object Object]
Body: [object Object]
If I attempt to log the data like this, the value of the "status" key logs correctly:
console.log("Status: " + req.body.status);
Any insights into why this might be happening?