铁路由器服务器端路由回调不工作 [英] Iron Router Server Side Routing callback doesn't work

查看:101
本文介绍了铁路由器服务器端路由回调不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是IronRouter的新版本,为什么readFile回调执行的响应发送到客户端。

  Router.map $ b() - > 
this.route'readFile',
path:'/ readFile'
其中:'server'
方法:'GET'
action:() - >
self = this
fs.readFile'/tmp/a.txt',(err,data) - >
if err
throw
console.log(data.toString())
self.response.writeHead(200,{'Content-Type':'text / plain'})
self.response.end data)
console.log('response ...')

  http.js:733 
W2049-12:04:26.781(8)? (STDERR)throw new Error('Can not not render headers after they are sent to the client。'
W2049-12:04:26.781(8)?(STDERR)^
W2049-12 :04:26.782(8)?(STDERR)错误:发送到客户端后无法渲染头。

但是,我使用express,就像这样工作很好。

  exports.index = function(req,res ){
fs.readFile('/ tmp / a.txt',function(err,data){
if(err)throw err;
console.log(data.toString() );
res.send(200,data.toString());
});
console.log('response ...');
};

感谢@ChristianF @Tarang使用Meteor._wrapAsync或Future都可以正常使用自定义函数replace fs如下所示:

  @getAccounts =(callback) > 
query =SELECT ID,Name,AccountNumber FROM Account
queryBySoql(query,(err,result) - >
if err
return console.error(err)
return callback(result)

我调用了链接:

 #使用Meteor 
#data = Meteor ._wrapAsync(getAccounts)()


#using Future
waiter = Future.wrap(getAccounts)()
data = waiter.wait()
this.response.writeHead 200,{'Content-Type':'text / plain'}
this.response.end JSON.stringify(data)
解决方案

只是今天我很努力与这非常问题。答案,在我看来,是流星(或至少铁路由器)不处理异步调用你所期望的方式。解决方案是将异步调用包装到光纤中,这是meteor用于保持编程同步的机制。



在你的情况下试试这个(对不起, t非常熟悉coffeescript):

  var Future = Npm.require('fibers / future'); 
...
var waiter = Future.wrap(fs.readFile);
var data = waiter('/ tmp / a.txt')。
response.writeHead(200,{'Content-Type':'text / plain'})
response.end(data)

EDIT 解决问题的补充。



有一个回调作为它的最后一个参数,有(err,result)签名。尝试此操作:

  @getAccounts =(callback) - > 
query =SELECT id,Name,AccountNumber FROM Account
queryBySoql(query,(err,result) - >
if err
return console.error(err)
return callback(err,result)


I am newer for IronRouter, why readFile callback executed the response are send to client.

Router.map(
  ()->
    this.route 'readFile',
      path: '/readFile'
      where: 'server'
      method: 'GET'
      action: ()->
        self = this
        fs.readFile '/tmp/a.txt', (err, data)->
          if err
            throw err
          console.log(data.toString())
          self.response.writeHead(200, {'Content-Type': 'text/plain'})
          self.response.end(data)
        console.log('response ...')

)

http.js:733
  W2049-12:04:26.781(8)? (STDERR)     throw new Error('Can\'t render headers after they are sent to the client.'
  W2049-12:04:26.781(8)? (STDERR)           ^
  W2049-12:04:26.782(8)? (STDERR) Error: Can't render headers after they are sent to the client.

But, I use express , like this is work well.

exports.index = function(req, res){
  fs.readFile('/tmp/a.txt', function (err, data) {
    if (err) throw err;
    console.log(data.toString());
    res.send(200, data.toString());
  });
  console.log('response ...');
};

thanks @ChristianF @Tarang Use Meteor._wrapAsync or Future all work well . when I use self define function replace fs.readFile. It take throw ex . I Doubt My defined function has error. As follows:

@getAccounts = (callback) ->
 query = "SELECT Id, Name, AccountNumber FROM Account"
 queryBySoql(query, (err, result)->
   if err
     return console.error(err)
   return callback(result)
)

I invoked link this:

# using Meteor
#data = Meteor._wrapAsync(getAccounts)()


#using Future
waiter = Future.wrap(getAccounts)()
data = waiter.wait()
this.response.writeHead 200, {'Content-Type': 'text/plain'}
this.response.end JSON.stringify(data)

thanks all.

解决方案

Just today I struggled with this very issue. The answer, it seems to me, is that meteor (or at least the iron router) doesn't handle async calls the way you'd expect. The solution is to wrap the async call into a fiber, which is the mechanism meteor uses to keep the programming synchronous.

In your case try this (sorry, I don't know coffeescript very well):

var Future = Npm.require('fibers/future');
...
var waiter = Future.wrap(fs.readFile);
var data = waiter('/tmp/a.txt').wait();
response.writeHead(200, {'Content-Type': 'text/plain'})
response.end(data)

EDIT Addressing the addition to the question.

Functions wrapped in a future need to have a callback as their last argument that has the (err, result) signature. Try this:

@getAccounts = (callback) ->
 query = "SELECT Id, Name, AccountNumber FROM Account"
 queryBySoql(query, (err, result)->
   if err
     return console.error(err)
   return callback(err, result)
)

这篇关于铁路由器服务器端路由回调不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆