我爲IronRouter,爲什麼READFILE回調執行被髮送到客戶端的響應更新。鐵路由器服務器端路由的回調不工作
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.
但是,我用快遞,像這樣的工作。
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都很好。當我使用自定義函數替換fs.readFile時。它採取前衝。我懷疑我定義的函數有錯誤。具體如下:
@getAccounts = (callback) ->
query = "SELECT Id, Name, AccountNumber FROM Account"
queryBySoql(query, (err, result)->
if err
return console.error(err)
return callback(result)
)
我調用此鏈接:
# 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)
感謝所有。
感謝。我知道了。 [Meteor._wrapAsync](https://www.eventedmind.com/feed/Ww3rQrHJo8FLgK7FF) – zhaoyou