node.js express.js发布数据

node.js express.js发布数据,express,body-parser,Express,Body Parser,我在获取客户端POST数据时遇到如下问题: http.createServer(function onRequest(request, response) { request.setEncoding("utf8"); var content = []; request.addListener("data", function(data) { content.push(data); //Collect the incoming

我在获取客户端POST数据时遇到如下问题:

http.createServer(function onRequest(request, response) {               
    request.setEncoding("utf8");
    var content = [];

    request.addListener("data", function(data) {
        content.push(data); //Collect the incoming data});

    //At the end of request call
    request.addListener("end", function() {
    response.writeHead( 200, {"Content-Type": "text/plain"} );
    ms = content[0];
    if(ms.toString() != "")
    {
        var msg = ms.toString();    //Parse the ms into string      
        console.log(msg); // Prints the message in the console
        var reqObj = JSON.parse(msg);   // If the incoming message is in JSON format, it can be parsed as JSON.
        console.log(reqObj); 
        response.end(); //Close the response
    }
});
{ "{\"account\": \"123456\"}": "" }
undefined
我的客户发送了:

content-type:application/x-www-form-urlencoded
内容:

{"value":"123456"}
在node.js中,将内容解析为json对象没有问题,我的代码如下:

http.createServer(function onRequest(request, response) {               
    request.setEncoding("utf8");
    var content = [];

    request.addListener("data", function(data) {
        content.push(data); //Collect the incoming data});

    //At the end of request call
    request.addListener("end", function() {
    response.writeHead( 200, {"Content-Type": "text/plain"} );
    ms = content[0];
    if(ms.toString() != "")
    {
        var msg = ms.toString();    //Parse the ms into string      
        console.log(msg); // Prints the message in the console
        var reqObj = JSON.parse(msg);   // If the incoming message is in JSON format, it can be parsed as JSON.
        console.log(reqObj); 
        response.end(); //Close the response
    }
});
{ "{\"account\": \"123456\"}": "" }
undefined
上述代码的输出: {“account”:“48264”}//解析为JSON之前 {account:'48264'}//解析为JSON后

但当我改成express.js并使用bodyParser时,它就错了

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.post('/', function(req, res, next) {
    req.setEncoding('utf8');
    console.log(req.body);
    console.log(req.body.value);
});
module.exports = router;
控制台中的上述输出如下所示:

http.createServer(function onRequest(request, response) {               
    request.setEncoding("utf8");
    var content = [];

    request.addListener("data", function(data) {
        content.push(data); //Collect the incoming data});

    //At the end of request call
    request.addListener("end", function() {
    response.writeHead( 200, {"Content-Type": "text/plain"} );
    ms = content[0];
    if(ms.toString() != "")
    {
        var msg = ms.toString();    //Parse the ms into string      
        console.log(msg); // Prints the message in the console
        var reqObj = JSON.parse(msg);   // If the incoming message is in JSON format, it can be parsed as JSON.
        console.log(reqObj); 
        response.end(); //Close the response
    }
});
{ "{\"account\": \"123456\"}": "" }
undefined
我在网上搜索了一下,但找不到答案。请帮忙。
提前感谢

在定义任何路由之前,您需要使用express body解析器中间件

var bodyParser = require('body-parser');
var app = express();
port = parseInt(process.env.PORT, 10) || 8080;

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// parse application/json
app.use(bodyParser.json());

app.listen(port);

app.post("/someRoute", function(req, res) {
  console.log(req.body);
  res.status(200).json({ status: 'SUCCESS' });
});

代码输出中有一个错误,应该是:{“value”:“48264”}我还在app.js app.use(bodyParser.json())中设置了bodyParser;use(bodyParser.urlencoded({extended:true}));你应该编辑你的问题,而不是评论你的更正。谢谢Kunal,我会尝试反馈Kunal,我检查过了,我已经在我的app.js中包含了你的代码。但还是不适合我。我会设法弄清楚会发生什么。谢谢。