API Structure

283 views
Skip to first unread message

Nick Parsons

unread,
Jan 4, 2013, 11:44:06 AM1/4/13
to res...@googlegroups.com
I'm new to Restify, and a little to Node. Looking to see if anyone has good recommendations for a file structure. Ideally, I'd like to move my routes, models, configs, etc. into their own directories, and autoload those into the main app.js file. Any examples, or pointers would be extremely helpful.

Thanks!

Ismael Gorissen

unread,
Jan 5, 2013, 6:16:12 AM1/5/13
to res...@googlegroups.com

Hi,

I'm using restify for 3 months now and node for 5 months. I'm not an expert (just a beginner) but I can help you with my example which is not the best solution. It is only a solution.

  • server.js
/// ------ MODULES DEPENDENCIES
var restify = require('restify');

/// ------ CUSTOM LIBRARIES
var api_1_0 = require('./versions/1.0');

/// ------ GLOBALS
var __NAME = 'ServerName';

/// ------ VARIABLES
var server = restify.createServer({
    name:__NAME
});

/// ------ COMMON HANDLERS
server.use(restify.bodyParser());
server.use(restify.queryParser());

/// ------ INITIALIZE ROUTES BY VERSIONS
api_1_0.init(server);

/// ------ RUN SERVER
server.listen(8080, function () {
    console.log("%s running on %s (%s)", server.name, server.url, server.log.fields.hostname);
});
  •  1.0.js
function init(server){
    var user = require('../services/1.0/user.js'),
        message = require('../services/1.0/message.js');

    /// ------------ USER ROUTES ------------
    /// ------ GET /user.json
    server.get('/1.0/user.json/:id', user.getUserInformation);
    server.get('/1.0/user.json', user.getUserInformation);
    /// ------ PUT /user.json
    server.put('/1.0/user.json/:id', user.updateUserInformation);
    server.put('/1.0/user.json', user.updateUserInformation);

    /// ------------ USER MESSAGES ROUTES ------------
    /// ------ GET /message.json
    server.get('/1.0/message.json/count/:id', message.getNumberOfMessagesForUser);
    server.get('/1.0/message.json/count', message.getNumberOfMessagesForUser);
    server.get('/1.0/message.json/:id', message.getUserMessageList);
    server.get('/1.0/message.json', message.getUserMessageList);
}

exports.init = init;
  • user.js
// modules dependencies

/**
 * Check if the user exists.
 *
 * @param req
 * @param res
 */
exports.getUserInformation = function getUserInformation(req, res) { // do something }

/**
 * Update or insert user information.
 *
 * @param req
 * @param res
 */
exports.updateUserInformation = function updateUserInformation(req, res) { // do something }
  • message.js
// modules dependencies

/**
 *
 * @param req
 * @param res
 */
exports.getNumberOfMessagesForUser = function getNumberOfMessagesForUser(req, res) { // do something }

/**
 *
 * @param req
 * @param res
 */
exports.getUserMessageList = function getUserMessageList(req, res) { // do something }

Aseem Kishore

unread,
Jan 5, 2013, 9:27:51 AM1/5/13
to res...@googlegroups.com
I organize my routes, models, configs, etc. into their own directories too. Node unfortunately doesn't have a "requireDir" capability built-in, but it's trivial to implement, and a while ago I did just that as an npm module:


And in fact, the motivation was very much for servers. Here's a simplified Restify app.coffee from one of my APIs:


Hope this helps!

Cheers,
Aseem

Nick Parsons

unread,
Jan 5, 2013, 11:19:42 AM1/5/13
to res...@googlegroups.com
Thank you, Aseeem. This is very helpful. Any chance you could show me how your routes & middleware files are structured? I'm also trying to figure out the best way to use module.exports, validation, etc. 
--
Nick Parsons | Web Developer | Movement Strategy

o: 303-442-2542 | c: 720-290-6665ni...@movementstrategy.com

Aseem Kishore

unread,
Jan 5, 2013, 11:22:08 AM1/5/13
to restify
Sure, they're structured just as you see on the `middleware` and `routes` objects in the code:

/middleware
- auth.coffee
- [others later perhaps]

/routes
- users.coffee
- things.coffee
- [others later perhaps]

Cheers,
Aseem

Nick Parsons

unread,
Jan 5, 2013, 11:25:22 AM1/5/13
to res...@googlegroups.com
Any possibility if a gist? :) not trying to take code, just hard to fully understand the best way to code out routes. Especially when introducing a middleware layer with with, validation, etc. 

Aseem Kishore

unread,
Jan 5, 2013, 11:33:17 AM1/5/13
to res...@googlegroups.com
Ok, updated the gist:


But it's pretty straightforward -- ultimately, you're just defining and exporting functions you'd pass to app.use(), app.get(), app.post(), etc.

Aseem

Nick Parsons

unread,
Jan 6, 2013, 10:24:00 AM1/6/13
to res...@googlegroups.com
Any possibility if a gist? :) not trying to take code, just hard to fully understand the best way to code out routes. Especially when introducing a middleware layer with with, validation, etc. 
Reply all
Reply to author
Forward
0 new messages