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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:17:39+00:00 2026-06-04T08:17:39+00:00

Suppose you have a simple block of code like this: app.get(‘/’, function(req, res){ res.send(‘Hello

  • 0

Suppose you have a simple block of code like this:

app.get('/', function(req, res){
    res.send('Hello World');
});

This function has two parameters, req and res, which represent the request and response objects respectively.

On the other hand, there are other functions with a third parameter called next. For example, lets have a look at the following code:

app.get('/users/:id?', function(req, res, next){ // Why do we need next?
    var id = req.params.id;
    if (id) {
        // do something
    } else {
        next(); // What is this doing?
    }
});

I can’t understand what the point of next() is or why its being used. In that example, if id doesn’t exist, what is next actually doing?

  • 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-04T08:17:41+00:00Added an answer on June 4, 2026 at 8:17 am

    It passes control to the next matching route. In the example you give, for instance, you might look up the user in the database if an id was given, and assign it to req.user.

    Below, you could have a route like:

    app.get('/users', function(req, res) {
      // check for and maybe do something with req.user
    });
    

    Since /users/123 will match the route in your example first, that will first check and find user 123; then /users can do something with the result of that.

    Route middleware is a more flexible and powerful tool, though, in my opinion, since it doesn’t rely on a particular URI scheme or route ordering. I’d be inclined to model the example shown like this, assuming a Users model with an async findOne():

    function loadUser(req, res, next) {
      if (req.params.userId) {
        Users.findOne({ id: req.params.userId }, function(err, user) {
          if (err) {
            next(new Error("Couldn't find user: " + err));
            return;
          }
    
          req.user = user;
          next();
        });
      } else {
        next();
      }
    }
    
    // ...
    
    app.get('/user/:userId', loadUser, function(req, res) {
      // do something with req.user
    });
    
    app.get('/users/:userId?', loadUser, function(req, res) {
      // if req.user was set, it's because userId was specified (and we found the user).
    });
    
    // Pretend there's a "loadItem()" which operates similarly, but with itemId.
    app.get('/item/:itemId/addTo/:userId', loadItem, loadUser, function(req, res) {
      req.user.items.append(req.item.name);
    });
    

    Being able to control flow like this is pretty handy. You might want to have certain pages only be available to users with an admin flag:

    /**
     * Only allows the page to be accessed if the user is an admin.
     * Requires use of `loadUser` middleware.
     */
    function requireAdmin(req, res, next) {
      if (!req.user || !req.user.admin) {
        next(new Error("Permission denied."));
        return;
      }
    
      next();
    }
    
    app.get('/top/secret', loadUser, requireAdmin, function(req, res) {
      res.send('blahblahblah');
    });
    

    Hope this gave you some inspiration!

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

Sidebar

Related Questions

I have one simple app that suppose to get information from user and send
Suppose you have a simple class like this: class foo{ private: int* mData; int
I have a problem with this very simple block of code. please give me
I suppose that this is an interesting code example. We have a class --
Suppose I have this simple contract which I've taken from MS examples and altered
suppose I have a simple container which have three element: <div> <span>hello world</span> <input
Suppose I have the following (trivially simple) base class: public class Simple { public
Lets suppose that I have the following simple query var q = from p
I have a very simple bit of code that is supposed to capture the
i have this class called MemoryManager, it is supposed to implement a simple smart

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.