我是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) )
更多推荐
铁路由器服务器端路由回调不工作
发布评论