M
M
MrAlkatras2018-04-07 15:32:08
Node.js
MrAlkatras, 2018-04-07 15:32:08

How to properly handle a file error?

Please tell me how to handle the error.

server.on("request", function(req, res) {
var file = fs.createReadStream(req.url || "index.html");
...
}

I write in it
file.on("error", function(err) {
console.log(err);
});

But it does not work (the queue does not reach it) if such a file does not exist for reading.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry221060, 2018-04-08
@MrAlkatras

Check again. This option works for me.

var file = fs.createReadStream("somefilethatnotexist.html");
file.on("error", (err) => {
    console.log('I got error!');
});

Another option is to try to hang the handler on the server
server.on("error", (err) => {
    console.log('I got error!');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question