loading...

Valid argument values for app.use( )

mtee profile image Margaret W.N ・1 min read

App.use( ) is an express method that mounts middleware functions in express applications. It takes two arguments, a path and a callback function (the middleware). If we intend to use the middleware every time a request is made regardless of the route, the path is ommited, leaving us with:

app.use(callback)

If the middeware is intended for a specific route the path must be included as an argument. There are number of different valid path values including but not limited to:

  • Path such as /users. This path defines any route beginning with /users
app.use('/users' ,callback)
  • Path pattern /user+profile. Any route beginning and ending with user and profile
app.use('/user+profile' ,callback)
  • An array. ['/users', '/profile'].
app.use(['/user', '/profile'], callback)

This are just but a few, you can find the full list in Express documentation.

The callback argument can be passed as a single function , multiple function or an array of functions.

app.use('/' ,callback)
app.use('/' ,callback, callback2)
app.use('/', [callback, callback2])

Note to myself: Tutorials are a quick way to get started but reading the documentation is very essential as much as it can be long and boring.

Day 38

Posted on by:

mtee profile

Margaret W.N

@mtee

Developer | GADs 20 scholar | Diving into backend

Discussion

pic
Editor guide