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.

Alosyius
  • 8,771
  • 26
  • 76
  • 120

2 Answers2

2

You may want to use the 'request' module, I am using it and I feel very comfortable with it.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
1

to expand on @Silviu Burcea 's recommendation of the request module:

//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') }
});
Plato
  • 10,812
  • 2
  • 41
  • 61