2013-02-11 130 views
3

如何在Node.js环境中发送以下请求?Node.js发送post请求的数据?

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

我试着去与Nginx的推流模块一起构建一个Node.js的服务器。

+0

从服务器或服务器发送? – 2013-02-11 08:04:42

+0

我想从Node.js的发送到Nginx的,所以我想上面的是我的Node.js服务器 – Alosyius 2013-02-11 08:05:10

+0

上执行也许这会帮助你? http://nodejs.org/api/http.html#http_http_request_options_callback – 2013-02-11 08:06:31

回答

2

您可能需要使用“请求”模块中,我使用它,我觉得很舒服。

+0

西尔维乌普,暨锅LUA legatura立方米齿。在legatura cu node.js :) – 2013-06-19 10:23:49

+0

@AlexandruRada am editat mail-ul la carerăspund。 :) – 2013-06-20 04:56:42

+0

n-am acces la el pe linkedid。 Iar aici nu il vad。女士 – 2013-06-20 10:33:58

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') } 
});