I'm trying to understand how RESTful APIs work. I'm trying to route a POST request with ExpressJS. My problem is that req.body.name
always returns undefined. In other words, I can't access data from the body of the HTTP request.
Here is the request I'm sending using Postman
And here is the error message send in the response
I've read through several articles and I understand that one must use some additional software (a.k.a middleware) to make the request body readable to Express (i.e parse the request). However I don't really understand why this is the case. Why can't I just access request.body.name
directly?
Here is my code
const express = require('express');
const app = express();
app.get('/', (req,res)=>{
console.log('Im in the index');
res.send('Index page');
});
app.get('/parameterRoute/:id', (req,res)=>{
console.log('Im in parameterRoute');
res.send(req.params.id);
});
// This is the problem
app.post('/postRoute',(req,res) =>{
res.send(req.body.name);
});