web-services - 在meteor中使用HTTP.get获取通过HTTP.post发布的数据

标签 web-services meteor service http-get httplistener

我使用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,它将处理身份验证和高级流管理(例如文件上传分块)等,但需要更多工作才能开始。

https://github.com/kahmali/meteor-restivus

关于web-services - 在meteor中使用HTTP.get获取通过HTTP.post发布的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45636017/

相关文章:

javascript - 在网站中加载外国内容比 document.write() 更好的做法?

javascript - 在 Meteor 中使用来自 Twitter 的流数据

linux - 无法通过 ssh 在远程服务器上启动屏幕

Web 服务中的 SQL 最佳实践

c# - IRS ACA 提交 - 错误 TPE1122,消息中的 WS 安全 header 无效

java - 如何将处理程序从 Activity 传递到服务

java - 如果我依赖于缓慢的服务,如何避免花费处理器时间来等待它?

WCF服务设计问题

java - Axis:记录 SOAP http 请求和响应

java - 将 Java Web 服务 WSDL 与 gsoap C 客户端一起使用时出现标记不匹配错误