javascript - How do I split up index.js file in a node/mean/express application? -
i've got 1 incredibly long index.js route file node/mongo/express application. here structure.
app.js /models schedule.js task.js etc... /routes index.js /public /javascript etc...
i want this
app.js /models schedule.js task.js etc... /routes index.js schedule.js tasks.js etc... /public /javascript etc...
how can split index.js file?
for example, have tried in app.js file
var routes = require('./routes/index'); var tasks = require('./routes/tasks'); // later in file app.use('/', routes); app.use('/tasks', tasks);
however, not finding task routes did before when had index.js. because took routes starting "/task" routes/index.js , placed them in routes/task.js, why express not recognizing routes in new file?
any advice @ helpful, friend , new whole meanjs way of doing things, , cobbled have various tutorials. desperately need refactor, because routes/index.js getting long work with. did best makes sense intuitively, out there give advice on how break our routes file?
well, answer question lies in how import (require in context) router instance. each 1 of files should module.exports
router object or similar routes can mounted on app instance create.
for example, have @ how (with yeoman's is) in project have on github here, refer how router object exported each route this example.
as example of doing this, here open source project i've been contributing here. notice have similar approach here, have @ example route declaration (with corresponding export) here.
Comments
Post a Comment