DEV Community 👩‍💻👨‍💻

Margaret W.N
Margaret W.N

Posted on

Valid argument values for app.use( )

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
  • Path pattern /user+profile. Any route beginning and ending with user and profile
app.use('/user+profile' ,callback)
Enter fullscreen mode Exit fullscreen mode
  • An array. ['/users', '/profile'].
app.use(['/user', '/profile'], callback)
Enter fullscreen mode Exit fullscreen mode

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])
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!