Extract request body information using app.use(req) – NodeJS Express

I am working on a NodeJS API and I have the below folder structure. I need to extract the request body information of an API call from the index.js itself before passing it to the stripeController.js.

  • index.js
    • Controllers
      • stripeController.js

I am using the below code segment for data extraction from index.js however it returns undefined as it cannot identify req.body at the time the request goes through the index.js.

app.use((req, res, next) => {
  if (req.originalUrl.startsWith('/api/stripeWebHook')) {

const stripeWebHook = require('./webhook/stripeWebHook')
app.use('/api/stripeWebHook', stripeWebHook)

module.exports = app

I can simply extract the body when it reaches the stripeController.js using the same attribute.

How can I resolve this?

>Solution :

Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser or multer.

const app = require('express')()
const bodyParser = require('body-parser')
const multer = require('multer') // v1.0.5
const upload = multer() // for parsing multipart/form-data

app.use(bodyParser.json()) // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

app.post('/', (req, res, next) => {

Leave a Reply