Using latest stable node.js and express from npm, I've created my first express project.
The default generated app defines routes/index.js, which contains a single route that renders the default index view.
I immediately assumed I could add other .js files to the routes/ folder and they would be included. This didn't pan out. Only routes/index.js is ever included. Adding additional routes to routes/index.js works fine.
What is the proper way to define and organize Express routes, following the structure provided by the express project generator?
The answer, paraphrasing the article at DailyJS:
Given the following routes:
app.get('/', function() {});
app.get('/users', function() {});
app.get('/users/:id', function() {});
... Create the following files:
routes/
├── index.js
├── main.js
└── users.js
Then, inside of routes/index.js:
require('./main');
require('./users');
For each new group of related routes, create a new file in routes/ and require() it from routes/index.js. Use main.js for routes that don't really fit in the other files.