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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:11:28+00:00 2026-05-26T04:11:28+00:00

I am beginning to work with Express JS and have run into an issue.

  • 0

I am beginning to work with Express JS and have run into an issue. I can’t seem to figure out the proper way to handle errors.

For example, I have a web services API that serves an object called “event”. I’d like to return a simple string of “cannot find event” when a user submits an event id that isn’t found. Here is how I’m currently structuring my code:

app.get('/event/:id', function(req, res, next) {
    if (req.params.id != 1) {
        next(new Error('cannot find event ' + req.params.id));
    }

    req.send('event found!');
});

When I submit an id other than 1, Node crashes with the following output:

http.js:527
   throw new Error("Can't set headers after they are sent.");
         ^
Error: Can't set headers after they are sent.
    at ServerResponse.<anonymous> (http.js:527:11)
    at ServerResponse.setHeader (/usr/local/kayak/node_modules/express/node_modules/connect/lib/patch.js:62:20)
    at /usr/local/kayak/node_modules/express/node_modules/connect/lib/middleware/errorHandler.js:72:19
    at [object Object].<anonymous> (fs.js:107:5)
    at [object Object].emit (events.js:61:17)
    at afterRead (fs.js:878:12)
    at wrapper (fs.js:245:17)

From what I can tell by using the node.js debugger, execution of the block of code continues after next() is called, meaning that req.send('event found!') tries to run. I don’t want this to happen.

The only workaround that I’ve found is to simply throw a new Error() instead of “next-ing” it, but this results in a default Express HTML error page being generated. I’d like a little more control than that.

I have taken the time to read over the error handling section of the Express documentation, but I couldn’t make sense of it.

  • 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-05-26T04:11:28+00:00Added an answer on May 26, 2026 at 4:11 am

    You’ll want to check out Express Error Handling. From there:

    app.param('userId', function(req, res, next, id) {
        User.get(id, function(err, user) {
            if (err) return next(err);
            if (!user) return next(new Error('failed to find user'));
            req.user = user;
            next();
        });
    });
    

    The sweetspot that you are missing is the return next(...)

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

Sidebar

Related Questions

This is a stupid question, but I can't work it out and it's beginning
I am just beginning to work on android and seems like I have got
I am beginning work on a large Visual Studio solution, and have come across
I'm beginning work on a web application that will have several distinct user types,
I am beginning to use Selenium-IDE, and I ran into an issue where the
I'm just beginning work with IIS, .NET, etc. so I apologize if any of
I am beginning work on a relatively large web-based application with the potential for
I am in the process of beginning work on several ASP.NET custom controls. I
I'm beginning to work on mailing-list software we use internally (EDIT: though we send
I am beginning to work on my first OpenGL iPhone app, but I've hit

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.