0

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}`);
});

1
  • I see you don't trust me, to use const insted of let :D The colleuge is right the query string module has nothing to do with the request body. Commented Nov 9, 2018 at 19:14

1 Answer 1

2

You should use: const par=JSON.parse(body)

instead of: let par=qs.parse(body);

"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',()=>{
           const par=JSON.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}`);
});

Sign up to request clarification or add additional context in comments.

5 Comments

I have used this statement const par=JSON.parse(body) It shows error like "SyntaxError: Unexpected end of JSON input
may be something is wrong in the request . I can post to /api/login with postman without errors. The request body is {"user":"Joe", "pass":"hjh"}
Yes,I did the mistake in postman.I should send the data from the body interface.Now it's working fine.and qs.parse () it's also working fine
body and chunk is a Buffer. So if I not mistaken, instead of JSON.parse(body) it should be JSON.parse(body.toString('utf-8'))
@gkont Actually I was wrong. body += c converts the Buffer to string (because body is a string). So no need to convert it once more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.