Post method API in Nodejs

Create the POST method API to insert the document in the MongoDB

Suppose, You have users collection which has three fields
1) _id
2) name
3) email


var MongoClient = require('mongodb').MongoClient;
var express = require('express');
var app = express();
app.post('/addUser', function (req, res) {

    MongoClient.connect(url, function (err, db) {

    if(!err){

    let req_body=req.body;
    let user_name=(typeof req_body.name !== 'undefined') ? (req_body.name).trim():"";
    let user_email=(typeof req_body.email !== 'undefined') ? (req_body.email).trim():"";

    let user_details={
    "name":user_name,
    "email":user_email
    }

    db.collection("users").insertOne(user_details,function(err, result) {

    if (err){
      res.json({data:result,messages:"some thing went wrong",status:501})
    }

   res.json({data:result,messages:"user add successfully",status:200})

    db.close();
  });

  }else{

   res.json({data:err,messages:"some thing went wrong",status:501})
  }

});

});

Note:- In the above example req.body is used to get the URL body parameters value.