Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Middleware Middleware Sequence and Routing

When would you use multiple app.use, and when would you use multiple functions in one app.use?

Thanks!

2 Answers

The documentation for app.METHOD() and app.use() explains. You would list multiple callbacks in a single app.use call when you want to bypass the remainder of the list with a call to next('route'). Check out the documentation for more information.

Kevin Gates
Kevin Gates
15,053 Points

This should be marked as Best Answer, in my opinion.

Tom Geraghty
Tom Geraghty
24,174 Points

I believe he is demonstrating that you can chain multiple functions to a single app.use and also that you can call app.use as many times as you want in a row and they will both act in the same, predictable way. As well as how multiple pieces of middleware can act on the same data stream.

In writing your own application it's likely best to use only one app.use with a chain of functions after it to reduce complexity.

Your team's style choice may be different especially if they write separate modules that all act on the same data.