DEV Community

Discussion on: Getting started with Mongoose discriminators in Express.js

tichel profile image

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

helenasometimes profile image
Nienke Author

Hi Peter,

I set up my routes in app.js like this:

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) {
        function(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) => {

Hope that helps any? I'm planning on open sourcing my code at some point, it just needs a lot of cleaning up :/