Everyone,I'm beginner in NODEJS I'm sending the data through post request from postman At some point of time I'm using querystring module.I'm parsing the post data .I want to print username and password in this code.but it's not printing.can anyone solve this issue? Thanks in advance...
"use strict";
let http=require('http');
let url=require("url");
let qs=require("querystring");
let port=process.env.PORT||8086;
let routes={
'GET':
{
'/':(req,res,urldata)=>
{
res.statusCode=200;
res.setHeader('content-Type','text/plain');
res.end("I 'm sending get request data " );
},
'/api/getinfo':(req,res,urldata)=>{
res.statusCode=200;
res.setHeader('content-Type','application/json');
console.log(JSON.stringify(urldata.query));
res.end(JSON.stringify(urldata.query));
}
},
'POST':
{
'/':(req,res,urldata)=>{
res.statusCode=200;
res.setHeader('content-Type','text/plain');
res.end("I 'm sending post request " );
},
'/api/login':(req,res,urldata)=>{
let body='';
req.on('data',(c)=>{
body+=c;
});
req.on('end',()=>{
let par=qs.parse(body);
console.log("USERNAME POST:"+par['user']);
console.log("PASSWORD POST:"+par['pass']);
res.end("ok I'm getting post data");
});
}
},
'NA':(req,res)=>{
res.statusCode=404;
res.end("Content Not found");
}
}
function handler(req,res)
{
if(req.url!="/favicon.ico")
{
let urldata=url.parse(req.url,true);
console.log(urldata);
console.log("Request Method:"+req.method);
console.log("Request URL:"+req.url);
console.log("Pathname:"+urldata.pathname);
let a=routes[req.method][urldata.pathname];
if(a!=undefined)
{
a(req,res,urldata);
}
else{
routes['NA'](req,res);
}
}
}
let server=http.createServer(handler);
server.listen(port,()=>{
console.log("server is listening on port",`${port}`);
});
constinsted oflet:D The colleuge is right the query string module has nothing to do with the request body.