Sam*_*mal 1 javascript http node.js
我已经知道使用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);
});
Run Code Online (Sandbox Code Playgroud)
我想知道如何在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
});
Run Code Online (Sandbox Code Playgroud)
请求库还有一个post in request.post方法的快捷方式,您可以在其中传递url以发送发布请求以及要发送到该URL的数据.
根据评论进行编辑
要"捕获"一个帖子请求,最好使用某种框架.由于快递是最受欢迎的,我将举一个快递的例子.如果您不熟悉快递,我建议您阅读作者自己的入门指南.
您需要做的就是创建一个post route,回调函数将包含发布到该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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11111 次 |
| 最近记录: |