我使用meteorjs构建了一个应用程序。我需要来自第三方的一些数据。他们会通过 HTTP.POST 方法向我发送数据。
我需要监听我的应用程序以查找 http.post 请求。如果我遇到这样的请求,我需要发送一个确认,表示已收到请求,然后我需要提取发送的数据。
我正在使用下面的代码,但输出不符合预期。
使用选择器包,
var postData = {
"channelName" : "Number Theory1",
"startDate" : "2017-07-22T06:29:35.681Z",
"endDate" : "2017-08-22T06:29:35.681Z"
}
HTTP.call('POST', 'http://localhost:3000', {
data: postData
}, (error, result) => {
if (error) {
console.log('we are getting this error:' + error);
} else {
console.log('POstres : ' + result);
}
});
function extractProcessData(data){
console.log('function called! : ' + data);
}
function confirmDataReceived(data) {
HTTP.get('http://localhost:3000', function(err, res){
// confirmation error
if(err){
console.log('error ' + err);
}
// confirmation success and process data
else{
console.log('data : ' + data + res)
extractProcessData(data) //call function to process data
}
});
}
var postRoutes = Picker.filter(Meteor.bindEnvironment(function(req, res) {
if (req.method == "POST"){
console.log('req : ' + req.method + " " + req.body)
confirmDataReceived(req.body);
}
return true;
// return req.method == "POST";
}));;;
如有任何帮助,我们将不胜感激!
干杯!
最佳答案
您需要使用中间件库。我过去用过 2 个。
选择器是最简单的实现和使用,就像
Picker.route('/post/:_id', function(params, req, res, next) {
var post = Posts.findOne(params._id);
res.end(post.content);
});
https://github.com/meteorhacks/picker
还有更强大的解决方案,Restivus,它将处理身份验证和高级流管理(例如文件上传分块)等,但需要更多工作才能开始。
关于web-services - 在meteor中使用HTTP.get获取通过HTTP.post发布的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45636017/