Debug POSTs using an Express App

04 December 2020

Updated: 03 September 2023

Sometimes it’s useful to have an endpoint that you can use to debug data that’s being POSTed to an application

You can make use of the following express.js app to log your application’s POST requests:

View Code
1
const express = require('express')
2
const app = express()
3
4
// parse json
5
app.use(express.json())
6
7
// GET endpoint to check uptime
8
app.get('/', (req, res) => {
9
res.json({ data: 'hello' })
10
})
11
12
// POST endpointthat logs request body
13
app.post('/', (req, res) => {
14
console.log(req.body)
15
res.json(req.body)
16
})
17
18
// listen for requests
19
const listener = app.listen(process.env.PORT, () => {
20
console.log('listening on port ' + listener.address().port)
21
})