铁路由器服务器端路由回调不工作

编程入门 行业动态 更新时间:2024-10-21 09:30:46
本文介绍了铁路由器服务器端路由回调不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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) )

更多推荐

铁路由器服务器端路由回调不工作

本文发布于:2023-10-12 17:08:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   回调   服务器端   铁路   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!