简体   繁体   English

Node.js中的HTTP POST无法正常工作

[英]HTTP POST in Node.js not working

From nodejs i am trying to post data to another URL 127.0.0.1:3002 (in file poster.js), but when i try to access it on server at 127.0.0.1:3002 then posted data is not coming: 从nodejs,我试图将数据发布到另一个URL 127.0.0.1:3002(在poster.js文件中),但是当我尝试在服务器上的127.0.0.1:3002上访问它时,发布的数据却不来了:

My poster.js looks like this: 我的poster.js看起来像这样:

var http = require('http');

function post() {

    var options = {
        host : '127.0.0.1',
        port : 3002,
        path : '/note/',
        method : 'POST'
    };

    var req = http.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            console.log('BODY: ' + chunk);
        });
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

    req.write("<some>xml</some>");
    req.end();
}

post();

and my server code in app.js is: 我在app.js中的服务器代码是:

var express=require('express');
app=express();

app.post('/note',function(req,res){
    console.log(req.params);
})

app.listen(3002);
console.log('sweety dolly')

my server console is showing: 我的服务器控制台显示:

sweety dolly
[]

req.params is showing [] that means it received nothing while sending i am sending xml req.params显示为[],表示发送时什么也没收到,我正在发送xml

in two different command line i am firing two different processes like 在两个不同的命令行中,我触发了两个不同的进程,例如

 node app

and then in next command line 然后在下一个命令行中

 node poster

What am i doing wrong???? 我究竟做错了什么????

Your client works, but when you POST , the data does not show up in params on the server by default (actually, params is for routing info) 您的客户端可以工作,但是当您POST ,默认情况下,数据不会显示在服务器上的params (实际上,params用于路由信息)

Since you're posting raw data, you'll need to collect the data yourself to use it, for example by use ing your own simple body parser; 由于您要发布原始数据,因此需要自己收集数据以使用它,例如,通过use自己的简单主体解析器;

var express=require('express');

app=express();

app.use(function(req, res, next) {
  var data = '';
  req.setEncoding('utf8');
    req.on('data', function(part) {      // while there is incoming data,
       data += part;                     // collect parts in `data` variable
    }); 

    req.on('end', function() {           // when request is done,
        req.raw_body = data;                 // save collected data in req.body
        next();
    });
});

app.post('/note',function(req,res){
    console.log(req.raw_body);               // use req.body that we set above here
})

app.listen(3002);
console.log('sweety dolly')

EDIT: If you want the data as a parameter, you'll need to change the client to post the data as a querystring with a name for the data; 编辑:如果要将数据作为参数,则需要更改客户端以将数据发布为带有数据名称的查询字符串。

var http = require('http');
var querystring = require('querystring');

function post() {

    var post_data = querystring.stringify({ xmldata: '<some>xml</some>' })

    var options = {
        host : '127.0.0.1', port : 3002, path : '/note/', method : 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': post_data.length
        }
    };

    var req = http.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            console.log('BODY: ' + chunk);
        });
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });


    req.write(post_data);
    req.end();
}

post();

Then you can just use the standard bodyParser to get the data from the param function; 然后,您可以使用标准的bodyParserparam函数获取数据。

var express=require('express');

app=express();

app.use(express.bodyParser());

app.post('/note',function(req,res){
    console.log(req.param('xmldata'));
})

app.listen(3002);
console.log('sweety dolly')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM