How do I get the POST data from a nuxtjs server middleware? So far I've managed to do it for GET, but for POST the body is just not there. req.body
is undefined.
3
votes
1 Answers
4
votes
Add this to nuxt.config.js
:
serverMiddleware: [
'~/api/v1/index.js'
],
and then create a file /api/v1/index.js
with:
const bodyParser = require('body-parser')
const app = require('express')()
module.exports = { path: '/api', handler: app }
app.use(bodyParser.json());
app.post('/newsletter/subscribe', (req, res) => {
res.json(req.body)
})
Key line is app.use(bodyParser.json())
Even if you are not using express, the code is still very similar.