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 8856917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:28:01+00:00 2026-06-14T14:28:01+00:00

I have app in Node.js and Express. I need to write tests for it.

  • 0

I have app in Node.js and Express. I need to write tests for it. I have a problem with handling Express app errors. I found this How do I catch node.js/express server errors like EADDRINUSE?, but it doesn’t work for me, I don’t know why. I want to handle errors, which can occured while expressApp.listen() is executing (EADDRINUSE, EACCES etc.).

express = require('express')
listener = express()

#doesn't work for me
listener.on('uncaughtException', (err) ->
  #do something
)

#doesn't work too
listener.on("error", (err) ->
  #do something
)

#this works, but it caughts all errors in process, I want only in listener
process.on('uncaughtException', (err) ->
  #do something
)

listener.listen(80) #for example 80 to get error

Any ideas?

  • 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-14T14:28:02+00:00Added an answer on June 14, 2026 at 2:28 pm

    First off, expressJS does not throw the uncaughtException event, process does, so it’s no surprise your code doesn’t work.

    So use: process.on('uncaughtException',handler) instead.

    Next, expressJS already provides a standard means of error handling which is to use the middleware function it provides for this purpose, as in:

    app.configure(function(){
        app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    });
    

    This function returns an error message to the client, with optional stacktrace, and is documented at connectJS errorHandler.

    (Note that errorHandler is actually part of connectJS and is only exposed by expressJS.)

    If the behavior the existing errorHandler provides is not sufficient for your needs, its source is located at connectJS’s errorHandler middleware and can be easily modified to suit your needs.

    Of course, rather than modifying this function directly, the “correct” way to do this is to create your own errorHandler, using the connectJS version as a starting point, as in:

    var myErrorHandler = function(err, req, res, next){
        ...
        // note, using the typical middleware pattern, we'd call next() here, but 
        // since this handler is a "provider", i.e. it terminates the request, we 
        // do not.
    };
    

    And install it into expressJS as:

    app.configure(function(){
        app.use(myErrorHandler);
    });
    

    See Just Connect it, Already for an explanation of connectJS’s idea of filter and provider middleware and How To Write Middleware for Connect/Express for a well-written tutorial.

    You might also find these useful:

    • How to handle code exceptions in node.js?

    • Recover from Uncaught Exception in Node.JS

    Finally, an excellent source of information regarding testing expressJS can be found in its own tests.

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

Sidebar

Related Questions

I have a JavaScript data structure like the following in my Node.js/Express web app:
I'm using Expressjs w/ node.js and I have a route like this: users.init(app.db, function()
I have a node app that has a string of require s, like this:
I have the following express node.js app. It's using the 'redis' npm package. app.get(/test,function(req,res){
I have the following node.js server-side code: var app = require('http').createServer(handler) , io =
I have an Ubuntu server on linode running a node.js/mongo app. I realize I
I have a Node.js Express app that I'm hosting on Nodejitsu. I'm already using
I have a node app with mongo server on an amazon ec2 instance. It
I have an express node app, and I'm trying to keep my code neat
I am trying to run some Mocha/Zombie.js tests on my new node.js (express) app.

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.