Skip to main content
ThunderLang™Programming Language

Gallery · JavaScript · Express

How Express adds middleware

Express is the classic Node.js web framework, and router.use() is how every middleware gets in. This is Express 4's version: it works out whether the first argument is a path or a function, refuses anything that is not a function, and puts one layer per middleware on the router's stack.

What to look for

Both ways it can stop: no middleware at all, or something in the list that is not a function.

How much Thunder could read

21 of 21 steps put into words.

Read by the current Thunder compiler when this page was built. Hand-checked against the code: accurate, with some wording that is thin or close to the code.

Flow: How does it go? One action or rule as a path: questions with Yes and No, repeats, the Stops, what happens if something goes wrong, and what always happens at the end.

Proto can

Use

From codelines 439–486
You give itfn
  1. Works out: Sets offset to 0
  2. Works out: Sets path to "/"
  3. Is fn a function?

    No

    1. Works out: Sets arg to fn
    2. For each: Repeats while Array.isArray(arg) and the length of arg is not 0

      For each

      1. Works out: Sets arg to `arg[0]`

      When done

    3. Is arg a function?

      No

      1. Works out: Sets offset to 1
      2. Works out: Sets path to fn

      Yes continue

    Yes continue

  4. Works out: Works out callbacks from flatten
  5. Is the length of callbacks 0?

    Yes

    1. Stop: TypeError: "Router.use() requires a middleware function"

    No continue

  6. For each: Counts i from 0 while i is less than the length of callbacks

    For each

    1. Finds: Sets fn to the i entry in callbacks
    2. Is fn a function?

      No

      1. Stop: TypeError: "Router.use() requires a middleware function but got a "

      Yes continue

    3. Does: Calls debug
    4. Works out: Creates a new Layer as layer
    5. Works out: Sets the layer's route to undefined
    6. Does: Adds layer to stack

    When done

  7. Gives back this
The same reading as Thunder Text, built with this page
Use (Proto)
  You give it: fn
  - Sets offset to 0  (line 440)
  - Sets path to "/"  (line 441)
  - Ask: Is fn a function?  (line 445)
    Yes: continue
    No:
      - Sets arg to fn  (line 446)
      - Repeats while Array.isArray(arg) and the length of arg is not 0  (line 448)
        For each:
          - Sets arg to `arg[0]`  (line 449)
      - Ask: Is arg a function?  (line 453)
        Yes: continue
        No:
          - Sets offset to 1  (line 454)
          - Sets path to fn  (line 455)
  - Works out callbacks from flatten  (line 459)
  - Ask: Is the length of callbacks 0?  (line 461)
    Yes:
      - Stop: TypeError: "Router.use() requires a middleware function"  (line 462)
    No: continue
  - Counts i from 0 while i is less than the length of callbacks  (line 465)
    For each:
      - Sets fn to the i entry in callbacks  (line 466)
      - Ask: Is fn a function?  (line 468)
        Yes: continue
        No:
          - Stop: TypeError: "Router.use() requires a middleware function but got a "  (line 469)
      - Calls debug  (line 473)
      - Creates a new Layer as layer  (line 475)
      - Sets the layer's route to undefined  (line 481)
      - Adds layer to stack  (line 483)
  - Gives back this  (line 486)

Source and license

From Express (expressjs/express), lib/router/index.js, lines 439–486, at commit 8e229f9 (4.21.1).

Copyright (c) 2009-2014 TJ Holowaychuk; Copyright (c) 2013-2014 Roman Shtylman; Copyright (c) 2014-2015 Douglas Christopher Wilson. Licensed under the MIT. The code is shown unmodified; the Thunder reading is ThunderLang's.

Full license text (LICENSE)
(The MIT License)

Copyright (c) 2009-2014 TJ Holowaychuk <tj@vision-media.ca>
Copyright (c) 2013-2014 Roman Shtylman <shtylman+expressjs@gmail.com>
Copyright (c) 2014-2015 Douglas Christopher Wilson <doug@somethingdoug.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.