Monday 4 March 2019

express: app.put(path, callback [, callback ...]): Route put requests


app.put(path, callback [, callback ...])
Routes HTTP PUT requests to the specified path with the specified callback functions.

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.put('/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: PUT
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' }




Previous                                                 Next                                                 Home

No comments:

Post a Comment