I do not quite understand how to properly break the logic on the controllers and models in nodeJS when working with the backend application. Suppose I have an example

This code is in the model of my application, and logically I understand that the model is only responsible for choosing from the database, and the controller and everything else should be done by the controller, but I don’t quite understand how to do this and I tried to transfer part of the code to the controller and export it, but I did not succeed (Please, help, at least with this example! The main thing for me is to understand the principle of working with MVC in the node !!!

As I wrote ->

postModejs.js

exports.homePage = function (req, res) { db.query('SELECT * FROM `posts`', function (err, result) { console.log(result); res.render('pages/home', {object: result}); }); }; exports.currentPostPage = function(req, res){ db.query('SELECT * FROM `posts`', function (err, result) { if (err){ console.log(err); } var post = result.filter(item => {return (item.id == req.params.id)? item: false})[0]; if (post === undefined){ res.render('pages/404'); } else { res.render('pages/post-page', {postId: req.params.id, item: post}); } }); }; 

server.js

 var express = require("express"); var bodyParser = require('body-parser'); var login = require('./controllers/login.controller'); var register = require('./controllers/register.controller'); var db = require('./db'); var pageroutes = require('./models/pageroutes'); var controllers = require('./controllers/new-post.controller'); var app = express(); app.set("view engine", "ejs"); app.use("/public", express.static("public")); app.use(bodyParser.urlencoded({ extended : false})); app.use(bodyParser.json()); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }); var router = express.Router(); //route to handle user registration router.post('/register', register.register); router.post('/login', login.login); app.use('/api', router); app.get('/register', pageroutes.registerPage); app.get('/login', pageroutes.loginPage); app.get('/', pageroutes.homePage); app.post('/', controllers.newPost); app.get('/post/:id', pageroutes.currentPostPage); app.get('/contact', pageroutes.contactPage); app.use(pageroutes.errorPage); app.listen(3000); 

    1 answer 1

    I propose to organize the code as follows:
    In the '/ users' route, the controller method is invoked. In the attached example, the controller is userController, and its method is called handleUsers.
    This method gets all users from userModel.getAll and selects all of them with id multiple of 5.
    Instead of accessing the database, the userModel uses a resource reference.

    Edit 0q641z9k0w

    • I did not understand anything) in the example are not users in posts) - Pavel Igorevich
    • @PavelIgorevich, my example is abstract. It is about users) And what exactly is not clear? The code on the link turned out to see? - eustatos
    • Yes, sorry, I will try to figure it out in the evening, I didn’t immediately understand it from the phone) - Pavel Igorevich