I have this simple code
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);
}
});
the file I am trying to send does not exist, which is a possible but fine exception. However, res.sendFile does not throw synchronous errors.
It doesn't appear that I can listen for errors this way:
res.sendFile(file).on('error', function(){})
// ?
So how can we handle errors thrown by res.sendFile?
Try this one, refer to the document 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);
}
});
Please have a detailed look at the documentation:
http://expressjs.com/en/4x/api.html#res.sendFile
According to the documentation, your code should look like this:
router.post('/foo', function(req,res,next){
var file = path.resolve(folder, runId, 'temp.html');
res.sendFile(file, options, function(err) {
if(err) {
res.status(err.status).end()
}
});
});
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With