Node.js http.createServer how to get error

后端 未结 4 466
广开言路
广开言路 2020-12-16 13:31

I am new to node.js and am trying to experiment with basic stuff.

My code is this

var http = require(\"http\");
http.createServer(function(request,          


        
相关标签:
4条回答
  • 2020-12-16 13:48

    You can use

    process.on('uncaughtException', function(e){
        console.log(e);
    });
    

    To handle uncaught exceptions. Any unhandled exception from the web server could be caught like this.

    0 讨论(0)
  • 2020-12-16 13:50

    You can do this by handling the error event on the server you are creating. First, get the result of .createServer().

    var server = http.createServer(function(request, response) {
    

    Then, you can easily handle errors:

    server.on('error', function (e) {
      // Handle your error here
      console.log(e);
    });
    
    0 讨论(0)
  • 2020-12-16 14:02

    The error is emitted in the listen() method. The API documentation includes an example just for your situtation.

    0 讨论(0)
  • 2020-12-16 14:06

    Print stacktrace of uncaught exceptions using following code.

    process.on('uncaughtException', function( err ) {
                console.error(err.stack);
     });
    
    0 讨论(0)
提交回复
热议问题