In this express POST example, we will see how to create a post service using express. The major difference of POST method compared to GET is that it will use the method post() in express object.

Express Post Example

The below example creates a post service. Take a look at the usage of “app.use(express.json())” in the code. This is used to parse the incoming JSON request. If we don’t use the body parsing middleware, it will return the body as undefined. The service expects a JSON and it also returns a JSON (Note the use of types()).

var express = require("express");
var app = express();
app.use(express.json())
app.post('/hello',function(req,res){
	console.log(req.body)
 	res.type('json');
	res.send('{"message":"Post Successful"}');
});
app.listen(8080);

Run the above post example using any client similar to Postman and you will get the response in JSON.

Express Post Example