node.js中的querystring(qs)模块无法解析?

时间:2018-11-09 18:38:08

标签: node.js query-string

每个人,我都是NODEJS的初学者,我是通过邮递员的发帖请求发送数据的。在某些时候,我正在使用querystring模块。我正在解析发帖的数据。我想在其中打印用户名和密码此代码。但它不打印。有人可以解决此问题吗?预先感谢...

"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 个答案:

答案 0 :(得分:1)

您应该使用:const par = JSON.parse(body)

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