When my program like this, the former middware can send string 'Hello world' to Postman.
app.use((req, res, next) => {
console.log(req.body);
res.send('Hello world');
next();
})
app.use(express.json());
})
But I don't know why when I put the test middleware after app.use(express.json()), it do not send anything. I use postman to send post request with raw data of type json.
CodePudding user response:
You must declare that middleware express.json before than any route handlers, because that middleware is in charge of parsing the information of the request and put into the request body property, after that it automatically call next to continue the execution flow. If you put that middleware at the end of your request, after the parsing of the information next will be called as you don't have more middleware to manage the execution flow your server hangs.
CodePudding user response:
OK, I just found that because my POSTMAN POST request use raw json with single quoted instead of double quoted. I changed it into double quoted so it worked. Thanks.
