Debug POSTs using an Express App

04 December 2020

Contents

    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
    const express = require("express")
    const app = express()
    
    // parse json
    app.use(express.json())  
    
    // GET endpoint to check uptime
    app.get('/', (req, res) => {
      res.json({ data: 'hello' })
    })
    
    // POST endpointthat logs request body
    app.post('/', (req, res) => {
      console.log(req.body)
      res.json(req.body)
    })
    
    // listen for requests
    const listener = app.listen(process.env.PORT, () => {
      console.log("listening on port " + listener.address().port)
    })