Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:13:55+00:00 2026-06-04T17:13:55+00:00

I want to res.end(‘server error’) in request if error. My code: http.createServer(function(req,res){ process.on(‘uncaughtException’, function()

  • 0

I want to res.end(‘server error’) in request if error. My code:

http.createServer(function(req,res){
  process.on('uncaughtException', function() {
    res.end('server error')
  })
  handlers(req,res)
}).listen(1337)

What’s wrong with my decision?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:13:57+00:00Added an answer on June 4, 2026 at 5:13 pm

    You’ll get an error/warning like this:

    (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
    Trace: 
        at EventEmitter.<anonymous> (events.js:139:15)
        at EventEmitter.<anonymous> (node.js:389:29)
        at Server.<anonymous> (/Users/taf2/work/phonetrac/services/ctm-pool/foo.js:2:11)
        at Server.emit (events.js:70:17)
        at HTTPParser.onIncoming (http.js:1572:12)
        at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:91:29)
        at Socket.ondata (http.js:1468:22)
        at TCP.onread (net.js:374:27)
    

    I’m going to assume you are doing the above because you noticed that when you started catching uncaughtException, client connections were no longer being closed so, on an error the client connection would never be released. If for example, you were embedding the node.js endpoint as a client side request in a script tag e.g.

    <script src="http://127.0.0.1:1337/yourendpoint.js"></script>
    

    This will hang the consumer of your service… hopefully you’re embedding your script using an async technique…

    Anyways, to handle the error using your technique can work provided you handle releasing the event listener.

    function handleError(err) {
      console.error("Caught exception:", err, err.stack);
      this.end();
      this.emit("cleanup");
    }
    
    require('http').createServer(function(req, res) {
      var errorHandler = handleError.bind(res)
      process.on("uncaughtException", errorHandler);
      res.on("cleanup", function() { process.removeListener("uncaughtException", errorHandler); });
    
      this.is.an.error;
    
      res.end();
      res.emit("cleanup");
    }).listen(1337);
    

    Trying it out we get no leaks, our errors are handled and the connections are closed leaving our clients free to move on to the next error:

    ab -n 100 -c 10 http://127.0.0.1:1337/
    

    However, this solution will break down under concurrency if your server does anything complex like the following:

    function handleError(err) {
      console.error("Caught exception:", err, err.stack);
      this.end();
      this.emit("error:cleanup");
    }
    
    require('http').createServer(function(req, res) {
      var errorHandler = handleError.bind(res)
      process.on("uncaughtException", errorHandler);
      res.on("error:cleanup", function() { process.removeListener("uncaughtException", errorHandler); });
    
      setTimeout(function() {
        this.is.an.error;
        res.end();
        res.emit("cleanup");
      },1000);
    
    }).listen(1337);
    

    The problem here is uncaughtException will be fired for all errors and they will be overlapping. This means for catching a global error like this your best to only have one process.on(“uncaughtException” handler.

    Instead in the above situation you’ll want to:

    • have an isolated try { } catch {} block – this can be difficult to get right.
    • allow the exception go uncaught and crash the process, use monit or something similar to restart the process.
    • you might try an array with a setInternval cleanup function to filter out closed connections.

    The following is using setInterval.

    var connectionIndex = 0;
    var connections = [];
    
    function handleError(err) {
      console.error("Caught exception:", err, err.stack);
      connections.forEach(function(conn) {
        conn.end();
      });
    }
    
    var server = require('http').createServer(function(req, res) {
      connections.push(res.connection);
    
      setTimeout(function() {
        this.is.an.error;
        res.end();
      },100);
    
    });
    
    setInterval(function() {
      console.log("conn check: %d", connections.length);
      connections = connections.filter(function(conn) {
        return !conn.destroyed;
      });
    },1000);
    
    process.on("uncaughtException", handleError);
    
    server.listen(1337);
    

    I kind of like the last solution, but I’m sure there might be edges to it that bite, so using the second solution have a monitoring service to restart your server is probably best.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to do a http.get request through node within my
I want to send wm_close to another process, with which i want end that
Want to use signal when follow() function called in UserProfile. I've written signal which
Want to play a file that on path like this /var/spool/sound/dog.wav present on server.
want to show a error message as a result of a form action. For
Want the function to sort the table by HP but if duplicate HPs then
I want to gzip a file hold it in memory and whenever a request
i have this full code: program List; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, Generics.Collections;
I've got a file called 'res' that's 29374 characters of http data in a
What i want to be able to do is have a server which one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.