1

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:

My poster.js looks like this:

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:

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

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????

4

1 回答 1

2

您的客户端可以工作,但是当您使用时POST,默认情况下数据不会显示在params服务器上(实际上,params 用于路由信息)

由于您要发布原始数据,因此您需要自己收集数据以使用它,例如通过使用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')

编辑:如果要将数据作为参数,则需要更改客户端以将数据作为查询字符串发布,并带有数据名称;

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();

然后您可以使用标准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')
于 2013-06-30T11:03:39.757 回答