Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,162 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