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'.

Simon Sporrong
Simon Sporrong
31,824 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 :)

Thanks a lot for your response. it clears up.