Skip to content
Kevin Hannigan edited this page Apr 23, 2017 · 2 revisions

The app/api folder holds the api routes for the application. The files in this folder allow the frontend application to communicate with the server. The different files are typically split up based on the main model the routes are related to. For the most part, these endpoints are simple CRUD operations.

This folder includes a file called app/api/api.js. This file is a sort of "index" file which eases the addition of new api files. The server.js file calls the api.js file, which in turn looks at the files in the api folder and sets up the routes for each one. This process looks like this:

in server.js:

56    //Set up the api endpoints
57    require(__dirname + '/app/api/api').init(express, app); 

Line 57 there calls the init function of the app/api/api module, passing in express and also the express app that we are setting up.

in api.js:

20    var init = function(express, app) {
21        fs
22        .readdirSync(__dirname)
23        .filter(function(file) {
24            file.indexOf(".") !== 0) && (file !== "api.js");
25        })
26        .forEach(function(file) {
27            //For each api module, call init
28            var router = express.Router();      //Create a new Router object
29            var newEndpoint = require(path.join(__dirname, file));
30            newEndpoint.init(router);           //Initialize the endpoints
31            app.use('/api/' + path.basename(file, '.js'), router); //Mount sub-api
32        });
33    };

We use node's fs module to read the files in the same directory as this one (aka the app/api directory). Then, we filter out a few kinds of files so that we don't call the function on invalid files. Then, for each file, we make a new express router, "require" the file that we want to include, and call init for that file, passing it the router object. The init function for an example api file might look like this:

In plan.js

201    var init = function(router) {
202        //Mounted on '/api/plan'
203        router.get('/getMine', ensureAuthenticated, endpoints.getMine);
204        router.get('/getPublic', ensureAuthenticated, endpoints.getPublic);
205        router.get('/load', ensureAuthenticated, endpoints.load);
206        router.get('/loadMostRecentPlan', ensureAuthenticated, endpoints.loadMostRecentPlan);
207        router.post('/save', ensureAuthenticated, endpoints.save);
208        router.post('/makePrivate', ensureAuthenticated, endpoints.makePrivate);
209        router.post('/makePublic', ensureAuthenticated, endpoints.makePublic);
210    };

The init function takes the router object and mounts some endpoints on it. Then, after calling the init function, back in api.js, we tell the express app to use the new router, and mount all of those routes under a parent route which is 'api/FILENAME'. Thus, all of our api routes follow the pattern http(s)://whatever-our-url-is.com/api/FILENAME/ROUTENAME

Clone this wiki locally