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 Build a REST API With Express Building API Routes in Express Using Middleware in Express

How does express determine the 'next' parameter to pass into each middleware function?

I'm curious as to how express knows what to pass as each next parameter. Is this simply done in order of app.use() functions, or is there more to it?

Thanks!

1 Answer

Tom Geraghty
Tom Geraghty
24,174 Points

The next parameter is a callback function. It's called next by convention to make writing and reading applications using this convention easier.

If you think in Promises, you can think of this particular callback as being called when the Promise has been resolved and the program is free to move on to the next step in processing the application's logic.

There's some useful info here: https://expressjs.com/en/guide/writing-middleware.html and here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise