Facebook Pixel

Where should business logic live in an Express app?

In controllers or services, not in routes. Routes should be thin: they declare the endpoint and bind it to a controller. router.get('/', listUsers) is good. Putting logic in the router makes the file hard to maintain.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Express Router: Grouping and Mounting Explained

For grouping routes by feature in separate files. You create a router in routes/users.js, add routes to it, and mount it in app.js with app.use('/users', usersRouter). This keeps app.js clean as features grow.

Use app.use('/path', router). The router's paths are then relative to the mount path. For example, router.get('/:id') mounted at /users responds to /users/:id.

Yes. Use router.use(middleware) at the top of the router file. Every route in the router now runs that middleware. Useful for auth and role checks across a feature.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.