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

  • Home
  • SEARCH
  • 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 9157187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:57:09+00:00 2026-06-17T12:57:09+00:00

This is my Express configuration code: var server = express() .use(express.cookieParser()) .use(express.session({secret: buffer.toString(‘hex’)})) .use(express.bodyParser())

  • 0

This is my Express configuration code:

var server = express()
    .use(express.cookieParser())
    .use(express.session({secret: buffer.toString('hex')}))
    .use(express.bodyParser())
    .use(express.static('./../'));

server.use(server.router);

It’s quite annoying that the last .use is not chained like the rest.

How can I do full chaining of my Express middleware definitions?

  • 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-17T12:57:10+00:00Added an answer on June 17, 2026 at 12:57 pm

    The express use implementation returns this every time and is thus always chainable. The chainability problem comes from your unnecessary use of server.use(server.router), which is automatically done for you by express when your first non-middleware route is added via server.get, server.post, etc. This can can be seen in the express source code here, although this is a common point of confusion and problems (most of which come from doing server.user(server.router) at all as opposed to just letting the router go where it goes by default, which is almost always the correct place for it. So here’s the forumla:

    1. Properly ordered server.use calls to set up your middleware
    2. Your specific routes with server.get('/home') and the other HTTP method functions
    3. Any error handling, 404 middleware, etc

    Express will add the router after 1 but before 2, and all will be well.

    In the comments you claim if you omit the server.router line, you lose access to req.session. Sorry, but this is simply not the case. Here’s an entire working express app that shows a middleware immediately before the session middleware having req.session undefined, and another one immediately after the session middleware having it exist.

        var express = require('express');
        var server = express();
        server.use(function (q,r,n) {console.log('Before cookie:', q.session);n();});
        server.use(express.cookieParser());
        server.use(function (q,r,n) {console.log('after cookie:', q.session);n();});
        server.use(express.session({secret: 'SEKRET'}));
        server.use(function (q,r,n) {console.log('after session:', q.session);n();});
        server.get('/', function (q,r,n) {r.send("you got slashed");});
        server.listen(3000);
    

    Run it and you see this on the console.

    Before cookie: undefined
    after cookie: undefined
    after session: { cookie: 
       { path: '/',
         _expires: null,
         originalMaxAge: null,
         httpOnly: true } }
    

    However, if you are asking why you can’t use the server variable as a function parameter in the same statement in which it is defined, that’s just how JavaScript works and has zero to do with express. You can’t do this:

    var foo = console.log(foo.bar);
    

    Because foo is undefined until that entire statement is complete.

    FWIW my opinion is that chainable code like this is dubious. It is unfriendly to debuggers and breakpoints, not supported as a first-class language feature, and isn’t really saving you any characters compared to each line being unindented and app.use(app.router), but so be it.

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

Sidebar

Related Questions

I have a basic Socket.io server setup like this: var server = express.createServer().listen(port); this.io
I've the following Server Side Code: var express = require('express') , http = require('http')
Why does SQL server express 2008 give me this error? CREATE TABLE model (
Why this code don't work,when i want run this code vwd 2008 express show
I have implemented this code, but fail at compilation (VC2008 Express Ed.) : Now
in vb 2008 express this option is available under application properties. does anyone know
I'm looking for ways to express this Python snippet in Perl: data = {A:
I am experiencing this problem with Node.js express framework 3.0: https://github.com/visionmedia/express/issues/1187 I've been using
I tried this in jade served by express but got unexpected identifier as an
I'm learning C++ and I'm using Visual C++ Express and while running this #include

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.