我有这个简单的代码
router.post('/foo', function(req,res,next){
try{
var file = path.resolve(folder, runId, 'temp.html');
res.sendFile(file);
}
catch(err{
//not reached
next(err);
}
});
Run Code Online (Sandbox Code Playgroud)
我试图发送的文件不存在,这是一个可能但很好的例外.但是,res.sendFile不会抛出同步错误.
似乎我不能以这种方式监听错误:
res.sendFile(file).on('error', function(){}) //?
那么我们如何处理res.sendFile引发的错误呢?
试试这个,参考文档res.sendFile
var options = {
root: __dirname + '/public/',
dotfiles: 'deny'
};
var fileName = path.resolve(folder, runId, 'temp.html');
res.sendFile(fileName, options, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
else {
console.log('Sent:', fileName);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3964 次 |
| 最近记录: |