0

Disclaimer: This is my first time: using node, creating a REST API, and trying out MVC server side. (so, just statistically speaking, I'm probably doing something wrong ¯\_(ツ)_/¯)

I'm working on creating a video sharing platform for use at my university.

It's going to be a single page application with client side routing. I plan on making ajax requests to a REST API for the CRUD functionality.

The basic structure I have going so far is that my "controllers" are receiving the routing requests and calling the model functions to interact with the DB. For example:

controllers/courses.js:

var express = require('express');
var router = express.Router();
var models = {};
models.courses = require('../models/courses.js');

router.get('/api/courses', models.courses.get_all);
router.get('/api/courses/:id', models.courses.get);
router.post('/api/courses', models.courses.create);
router.put('/api/courses/:id', models.courses.update);
router.delete('/api/courses/:id', models.courses.delete);

models/courses.js:

var db = require('../config/db_config.js');

exports.get_all = function (req, res, next) {
    db.any('select * from course')
    .then(function (data) {
        res.status(200).json({
            status: 'success',
            data: data,
            message: 'Retrieved all courses'
        });
    })
    .catch(function (err) {
        return next(err);
    });
};

exports.get = function (req, res, next) {
    db.one('select * from course where id = $1', req.params.id)
    .then(function (data) {
        res.status(200).json({
            status: 'success',
            data: data,
            message: 'Retrieved a course'
        });
    })
    .catch(function (err) {
        console.log(err);
        return next(err);
    });
};

Is this more or less the right way to do it? Do you forsee me running into issues down the road?

My gut says that the model code should be passing the query results back to the controller, rather than directly back to the client. Is this correct? If so, how would I go about that?

Tell me what you would do differently.

1

Here is how MVC is intended to work...

The controller receives the request from the client, performs any business rules logic to determine and implement the changes to the model that are required. The final part is that it selects the appropriate model and view to return to the client. The View defines the presentation of the model to the client.

I have a second observation to make. If you implement a REST api to deliver a CRUD service you need to be in complete control of all clients that can connect to the REST API. If, as it seems from your question, that the clients are javascript code running in the web browser, then you cannot be in control of all clients, as someone could write their own client to access your CRUD API, and manipulate the back end objects as they see fit.

Hopefully, you mean that you are implementing a REST API that includes the business rules authenticating and validating requests as well as performing object updates.

  • Yes, I just realized that for my controllers to be doing authentication, I need to separate the notions of routing and controllers. (Although choosing a view isn't necessary as this is an SPA, the controllers just need to be concerned with providing the JSON data.) My current scheme is basically that the client's request hits a route, which passes the data along to a controller. The controller authenticates, gets data from the model, does other business logic and then sends the response directly to the client. How's that sound? – Luke Apr 2 '16 at 11:15
  • Actually, should the route be doing the authentication instead of the controller? – Luke Apr 2 '16 at 11:21
  • I'm not familiar with the programming language you are using, but in general, there can be multiple routes to the controller, so it is much better to place all the request processing on the controller, and just let routes deal with identifying which controller and process any rewrite rules. – Michael Shaw Apr 2 '16 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.