set specific bodyParser for just one route express

Viewed 2515

I set app.use(bodyParser.json()); in my app.js (main file). I need to change with bodyParser.text() in another file (foo.js) just for one route. External service send me a request POST 'Content-Type: text/plain; charset=utf-8' and I need to retrieve the content

3 Answers

If you're coming here circa 2021 or later, here is how I set up my app.js file for this issue.

// Import your various routers
const stripeEventsRouter = require('./routes/stripeEvents')
const usersRouter = require('./routes/users')
const commentsRouter = require('./routes/comments')

// use raw parser for stripe events
app.use('/stripeEvents', express.raw({ type: '*/*' }), stripeEventsRouter)

// use express.json parser for all other routes
app.use(express.json())

// set up the rest of the routes
app.use('/users', usersRouter)
app.use('/comments', commentsRouter)
// ...anything else you add below will use json parser
Related