Reputation: 161
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
Upvotes: 4
Views: 5366
Reputation: 5051
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
Upvotes: 8
Reputation: 1189
For express 4XX, you do not need to install body-parser anymore:
router.use('/endpoint', express.text(), itemRoute);
You can read more here: http://expressjs.com/en/api.html#express.text
Upvotes: 1
Reputation: 2531
See the following API Doc at https://expressjs.com/en/5x/api.html#app.METHOD
You can add middleware(s) to a specific route:
app.get('/', bodyParser.text(), function (req, res) {
...
})
Upvotes: 5