2013-02-11 129 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') } 
});