Javascript 向Node.JS中的HTTP POST请求添加参数

Javascript 向Node.JS中的HTTP POST请求添加参数,javascript,node.js,http,Javascript,Node.js,Http,我知道使用Node.js发送简单HTTP请求的方法如下: var http = require('http'); var options = { host: 'example.com', port: 80, path: '/foo.html' }; http.get(options, function(resp){ resp.on('data', function(chunk){ //do something with chunk }); }).on("error

我知道使用Node.js发送简单HTTP请求的方法如下:

var http = require('http');

var options = {
  host: 'example.com',
  port: 80,
  path: '/foo.html'
};

http.get(options, function(resp){
  resp.on('data', function(chunk){
    //do something with chunk
  });
}).on("error", function(e){
  console.log("Got error: " + e.message);
});
我想知道如何在
POST
请求的主体中嵌入参数,以及如何从接收器模块捕获参数。

您是否介意使用。发送post请求变得非常简单

var options = {
url: 'https://someurl.com',
'method': 'POST',
 'body': {"key":"val"} 

};

 request(options,function(error,response,body){
   //do what you want with this callback functon
});
请求库还有一个在
request.post
方法中进行post的快捷方式,在该方法中,您可以将要发出post请求的url以及要发送到该url的数据一起传递给该url

根据评论进行编辑

要“捕获”post请求,最好使用某种框架。因为它是最流行的一种,我将举一个express的例子。如果你不熟悉express,我建议你自己读一本

您只需创建一个post路由,回调函数将包含发布到该url的数据

app.post('/name-of-route',function(req,res){
 console.log(req.body);
//req.body contains the post data that you posted to the url 
 });
你介意用这个吗。发送post请求变得非常简单

var options = {
url: 'https://someurl.com',
'method': 'POST',
 'body': {"key":"val"} 

};

 request(options,function(error,response,body){
   //do what you want with this callback functon
});
请求库还有一个在
request.post
方法中进行post的快捷方式,在该方法中,您可以将要发出post请求的url以及要发送到该url的数据一起传递给该url

根据评论进行编辑

要“捕获”post请求,最好使用某种框架。因为它是最流行的一种,我将举一个express的例子。如果你不熟悉express,我建议你自己读一本

您只需创建一个post路由,回调函数将包含发布到该url的数据

app.post('/name-of-route',function(req,res){
 console.log(req.body);
//req.body contains the post data that you posted to the url 
 });

如果您不熟悉此功能,甚至不要尝试使用
http
code模块来完成此功能,而是使用更友好的框架,如
express
或其他模块。如果您不熟悉此功能,甚至不要尝试使用
http
code模块来实现这一点,而是使用更友好的框架,如
express
或其他模块。这很好,但是如何在接收方捕获这些身体参数呢?这很好,但是如何在接收方捕获这些身体参数呢?