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 Parameters, Query Strings, and Modularizing Routes Modular Routes

app.use

so basically app.use kinda imports the usage of the module? as in this case we have a routes module which contains all the routes. we exports the routes in the routes/index.js. then we require it in the main app.js file

const routes = ('./routes');
//as by default we have a index.js file  so the whole index.js file will, and we are exporting //the whole router function
const router = express.Router();
module.exports = router;
// here we export the whole router function,whatever middlewares and routes it has.
// hence the router will be //exported.

now in the main app.

const routes = require('./routes');
app.use(routes);

and now this router is connected to the main application. my questions are : is my understanding correct? we can import as many modules we'd like via the app.use method? as long as the routes don't collide with each other working. such as two '/' paths or 'about'.

Thanks a lot for your response. it clears up.

1 Answer

Simon Sporrong
Simon Sporrong
35,097 Points

Hi there!

I played around a little bit with the routes and you are correct. We can call any number of routes on the app.use method. You can even have more than one route that res.renders / res.sends to the same page. Then on that page you can make a conditional statement and check which content to show, (in pug at least). As we did in previous videos.

Hope that helps somewhat :)