Nienke, great article that helped me a lot! What would be the routes from app.js to this Schemes.. ? Routes via models/book.js, models/tvshow.js or via the models.base.js? Many thanks, Peter Tichelaar
Hi Peter,
I set up my routes in app.js like this:
app.js
const routes = require('./routes/routes'); // bunch of middleware app.use('/', routes);
And then in routes.js, I call my controllers:
const base_controller = require('../controllers/baseController'); const creation_controller = require('../controllers/creationController');
and inside baseController, I call my models like this:
const Book = require('../models/book'); const Movie = require('../models/movie'); const Tvshow = require('../models/tvshow'); const Base = require('../models/base');
If I want to, for example, get an item by its ID, I do this inside my controller:
// Get item by ID exports.get_item_by_id = function(req, res, next) { async.parallel({ function(callback) { Base.findById(req.params.id) .exec(callback); }, }, function(err, results) { if (err) { return next(err); } res.render('templates/update', { data: results }); }); };
Then in my routes, I do this:
router.get('/update/:id', ensureAuthenticated, base_controller.get_item_by_id, (req, res) => { res.send(req.params); });
Hope that helps any? I'm planning on open sourcing my code at some point, it just needs a lot of cleaning up :/
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nienke, great article that helped me a lot! What would be the routes from app.js to this Schemes.. ? Routes via models/book.js, models/tvshow.js or via the models.base.js? Many thanks, Peter Tichelaar
Hi Peter,
I set up my routes in
app.js
like this:And then in routes.js, I call my controllers:
and inside baseController, I call my models like this:
If I want to, for example, get an item by its ID, I do this inside my controller:
Then in my routes, I do this:
Hope that helps any? I'm planning on open sourcing my code at some point, it just needs a lot of cleaning up :/