Node.js send a request with data?

How can I send the following request to the Node.js environment?

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

      

I am trying to create a Node.js server along with a Push Stream module for Nginx.

+3


source to share


2 answers


You can use the query module, I use it and I feel very comfortable with it.



+2


source


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

      

+1


source







All Articles