Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The app object is instantiated on creation of the Express server. It has a <em><strong>middleware</strong> stack</em> that can be customized in <a href="https://github.com/strongloop/express/wiki/Migrating-from-3.x-to-4.x" rel="noreferrer"><code>app.configure()</code>(this is now deprecated in version 4.x)</a>. </p> <p>To setup your middleware, you can invoke <a href="http://expressjs.com/en/4x/api.html#app.use" rel="noreferrer"><code>app.use(&lt;specific_middleware_layer_here&gt;)</code></a> for every middleware layer that you want to add (it can be generic to all paths, or triggered only on specific path(s) your server handles), and it will add onto your <a href="http://expressjs.com/en/guide/routing.html" rel="noreferrer">Express</a> middleware stack. Middleware layers can be added one by one in multiple invocations of <code>use</code>, or even all at once in series with one invocation. See <a href="http://expressjs.com/en/4x/api.html#app.use" rel="noreferrer"><code>use</code> documentation</a> for more details.</p> <p>To give an example for conceptual understanding of Express Middleware, here is what my app middleware stack (app.stack) looks like when logging my app object to the console as JSON:</p> <pre><code>stack: [ { route: '', handle: [Function] }, { route: '', handle: [Function: static] }, { route: '', handle: [Function: bodyParser] }, { route: '', handle: [Function: cookieParser] }, { route: '', handle: [Function: session] }, { route: '', handle: [Function: methodOverride] }, { route: '', handle: [Function] }, { route: '', handle: [Function] } ] </code></pre> <p>As you might be able to deduce, I called <code>app.use(express.bodyParser())</code>, <code>app.use(express.cookieParser())</code>, etc, which added these express middleware 'layers' to the middleware stack. Notice that the routes are blank, meaning that when I added those middleware layers I specified that they be triggered on any route. If I added a custom middleware layer that only triggered on the path <code>/user/:id</code> that would be reflected as a string in the <code>route</code> field of that middleware layer object in the stack printout above.</p> <p><em>Each layer is essentially adding a function that specifically handles something to your flow through the middleware.</em></p> <p>E.g. by adding <code>bodyParser</code>, <em>you're ensuring your server handles incoming requests through the express middleware</em>. So, <em>now parsing the body of incoming requests is part of the procedure that your middleware takes when handling incoming requests</em> -- all because you called <code>app.use(bodyParser)</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload