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 Asynchronous Code in Express Asynchronous Code in Express Reduce Error Handling Code When Using Async/Await

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

Could we mount the asyncHandler middleware with app.use()?

I love this nifty asyncHandler middleware so that we don't have to write out try/catch each time.

Just wondering if it would be possible to mount this middleware with app.use(), so we didn't even have to wrap each function in the route handlers later on.

1 Answer

odzer
odzer
20,483 Points

I don't think so, as its purpose is to wrap the route handler function. If inserted before the handler, I don't think there is anything to wrap there. As far as I understand, the middleware is supposed to make changes on the request object and then pass it along.