3

How can i send the following request in a Node.js environment?

curl -s -v -X POST 'http://localhost/pub?id=my_channel_1' -d 'Hello World!'

Im trying to build a Node.js server together with the Nginx Push Stream Module.

4

2 回答 2

2

您可能想使用“请求”模块,我正在使用它,我觉得它很舒服。

于 2013-02-11T09:21:16.573 回答
1

扩展@Silviu Burcea 对请求模块的建议:

//Set up http server:
function handler(req,res){ 
  console.log(req.method+'@ '+req.url);
  res.writeHead(200); res.end();
};
require('http').createServer(handler).listen(3333);

// Send post request to http server
// curl -s -v -X POST 'http://localhost/pub?id=my_channel_1' -d 'Hello World!'
// npm install request (https://github.com/mikeal/request)
var request = require('request'); 
request(
{ uri:'http://localhost:3333/pub?id=my_channel_1',
  method:'POST',
  body:'Hello World!',
},
function (error, response, body) {
  if (!error && response.statusCode == 200) { console.log('Success') }
});
于 2013-02-11T14:17:30.787 回答