javascript - how to stop node js server from crashing - Stack Overflow

I am new to node js. I was trying to create a simple HTTP server. I followed the famous example and cre

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 :(

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 :(

Share Improve this question edited Jan 13, 2013 at 17:48 Goje87 asked Jan 13, 2013 at 17:21 Goje87Goje87 2,8697 gold badges30 silver badges49 bronze badges 2
  • 2 See stackoverflow./a/8114990/435413 (autoreboot javascript) or stackoverflow./questions/5999373/… (prevent crash) – sod Commented Jan 13, 2013 at 18:01
  • 1 Hi Benjamin, I purposefully typed it as res1 to generate error. My question is about how to handle such errors without crashing the server. – Goje87 Commented Jan 13, 2013 at 18:45
Add a ment  | 

3 Answers 3

Reset to default 6

There are a bunch of ments here. First of all, for your example server to work, handleRequest needs to be defined BEFORE using it.

1- What you actually want, which is preventing the process to exit, can be handled by handling uncaughtException (documentation) event:

var handleRequest = function(req, res) {
    res.writeHead(200);
    res1.end('Hello, World!\n');
};
var server = require('http').createServer(handleRequest);
process.on('uncaughtException', function(ex) {
    // do something with exception
});
server.listen(8080);
console.log('Server started on port 8080');

2- I would rement to use try{} catch(e) {} on your code, such as:

var handleRequest = function(req, res) {
    try {
      res.writeHead(200);
      res1.end('Hello, World!\n');
    } catch(e) {
      res.writeHead(200);
      res.end('Boo');
    }
};

3- I guess the example was just an example and not actual code, this is a parsing error that can be prevented. I mention this, since you NEED to NOT have parsing errors on Exception catch handlers.

4- Please note that node process is going to be replaced in the future with domain

5- I'd rather use a framework like express, than doing this stuff.

6- Remended lecture: StackOverflow - NodeJS best practice for exception handling

I am not targeting your question details but your question's title about preventing Node server from crashing. You can probably use DOMAIN, this will probably stop your server from crashing when an uncaughtException is thrown.

domain = require('domain'),
d = domain.create();

d.on('error', function(err) {
  console.error(err);
});

for more details go http://shapeshed./uncaught-exceptions-in-node/

beside using this method must try-catch your block.

Maybe you should define handleRequest before you use it:

require('http').createServer(handleRequest).listen(8080);

function handleRequest(req, res) {
  res.writeHead(200);
  res1.end('Hello, World!\n');
};

Or

var handleRequest = function(req, res) {
  res.writeHead(200);
  res1.end('Hello, World!\n');
};

require('http').createServer(handleRequest).listen(8080);

And you should be sure that res1 also exists.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743738880a4498771.html

相关推荐

  • javascript - how to stop node js server from crashing - Stack Overflow

    I am new to node js. I was trying to create a simple HTTP server. I followed the famous example and cre

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信