Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@branneman
Last active December 30, 2015 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save branneman/7890425 to your computer and use it in GitHub Desktop.
Save branneman/7890425 to your computer and use it in GitHub Desktop.
Example code style I use of a Node.js module. Uses @felixge's Node Style Guide and some more conventions on naming and formatting.
//
// Articles controller
// Optional module description line.
//
'use strict';
var fs = require('fs');
var path = require('path');
var Articles = require('./models/articles');
var Comments = require('./models/comments');
// Expose module
module.exports.index = indexAction;
module.exports.archive = archiveAction;
// Renders the latest articles
function indexAction(req, res) {
_render(res, 'index', Articles.getLatest());
}
// Renders the archive, articles older than a year
function archiveAction(req, res) {
_render(res, 'archive', Articles.getArchive());
}
// Render view
function _render(res, file, articles) {
res.render('articles/' + file + '.html', {
articles: articles
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment