I am new to node js. I was trying to create a simple HTTP server. I followed the famous example and created a 'Hello World!' server as follows.
var handleRequest = function(req, res) {
res.writeHead(200);
res1.end('Hello, World!\n');
};
require('http').createServer(handleRequest).listen(8080);
console.log('Server started on port 8080');
Running this code would start the server properly as expected. But trying to access http://127.0.0.1:8080
would crash it by throwing an error that res1
is not defined. I would like to have the server still continue running and gracefully report errors whenever it encounters it.
How do I achieve it? I tried try-catch but that isn't helping me :(