app.post(path,
callback [, callback ...])
Route post requests to this path to the callback
functions.
Example
app.post('/user', (req, res) => {
console.log(req.body);
res.json(req.body);
})
index.js
var express = require('express') var bodyParser = require('body-parser'); var app = express() // for parsing application/json app.use(bodyParser.json()); app.get('/', (req, res) => { res.send('Hello World') }) app.post('/user', (req, res) => { console.log(req.body); res.json(req.body); }) var listener = app.listen(3000, () => {console.log('Application started on port ' + listener.address().port);});
Run index.js.
Use any rest client like postman to send post requests.
Method: POST
body:
{
"name"
: "Krishna",
"id" :
"I12345"
}
Set 'Content-Type' header to application/json.
You can see below response.
You can see below messages in console.
Application started on port 3000
{ name: 'Krishna', id: 'I12345' }
No comments:
Post a Comment