Code cleanup, updated jsdoc comments, added missing "next" error middleware to api.js controllers.

This commit is contained in:
Sahat Yalkabov
2014-01-13 04:24:31 -05:00
parent 9d21bdea83
commit 38245ccfb8
4 changed files with 38 additions and 24 deletions

14
app.js
View File

@ -12,7 +12,7 @@ var MongoStore = require('connect-mongo')(express);
var passport = require('passport'); var passport = require('passport');
/** /**
* Controllers. * Load controllers.
*/ */
var homeController = require('./controllers/home'); var homeController = require('./controllers/home');
@ -21,19 +21,19 @@ var apiController = require('./controllers/api');
var contactController = require('./controllers/contact'); var contactController = require('./controllers/contact');
/** /**
* API keys and Passport configuration. * API keys + Passport configuration.
*/ */
var secrets = require('./config/secrets'); var secrets = require('./config/secrets');
var passportConf = require('./config/passport'); var passportConf = require('./config/passport');
/** /**
* Connect to MongoDB. * Mongoose configuration.
*/ */
var opts = { server: { auto_reconnect: true } };
mongoose.connect(secrets.db, opts); mongoose.connect(secrets.db);
mongoose.connection.on('error', function() { mongoose.connection.on('error', function() {
console.log('MongoDB Connection Error'); console.log('MongoDB Connection Error');
}); });
var app = express(); var app = express();
@ -72,7 +72,7 @@ app.use(function(req, res) {
app.use(express.errorHandler()); app.use(express.errorHandler());
/** /**
* Routes. * Application routes.
*/ */
app.get('/', homeController.index); app.get('/', homeController.index);

View File

@ -15,8 +15,9 @@ var Twit = require('twit');
/** /**
* GET /api * GET /api
* List of API examples * List of API examples.
*/ */
exports.getApi = function(req, res) { exports.getApi = function(req, res) {
res.render('api/index', { res.render('api/index', {
title: 'API Browser' title: 'API Browser'
@ -25,9 +26,10 @@ exports.getApi = function(req, res) {
/** /**
* GET /api/foursquare * GET /api/foursquare
* Foursquare API example * Foursquare API example.
*/ */
exports.getFoursquare = function(req, res) {
exports.getFoursquare = function(req, res, next) {
var token = _.findWhere(req.user.tokens, { kind: 'foursquare' }); var token = _.findWhere(req.user.tokens, { kind: 'foursquare' });
async.parallel({ async.parallel({
trendingVenues: function(callback) { trendingVenues: function(callback) {
@ -62,8 +64,9 @@ exports.getFoursquare = function(req, res) {
/** /**
* GET /api/tumblr * GET /api/tumblr
* Tumblr API example * Tumblr API example.
*/ */
exports.getTumblr = function(req, res) { exports.getTumblr = function(req, res) {
var token = _.findWhere(req.user.tokens, { kind: 'tumblr' }); var token = _.findWhere(req.user.tokens, { kind: 'tumblr' });
var client = tumblr.createClient({ var client = tumblr.createClient({
@ -83,8 +86,9 @@ exports.getTumblr = function(req, res) {
/** /**
* GET /api/facebook * GET /api/facebook
* Facebook API example * Facebook API example.
*/ */
exports.getFacebook = function(req, res, next) { exports.getFacebook = function(req, res, next) {
var token = _.findWhere(req.user.tokens, { kind: 'facebook' }); var token = _.findWhere(req.user.tokens, { kind: 'facebook' });
graph.setAccessToken(token.accessToken); graph.setAccessToken(token.accessToken);
@ -112,8 +116,9 @@ exports.getFacebook = function(req, res, next) {
/** /**
* GET /api/scraping * GET /api/scraping
* Web scraping example using Cheerio library * Web scraping example using Cheerio library.
*/ */
exports.getScraping = function(req, res, next) { exports.getScraping = function(req, res, next) {
request.get('https://news.ycombinator.com/', function(err, request, body) { request.get('https://news.ycombinator.com/', function(err, request, body) {
if (err) return next(err); if (err) return next(err);
@ -131,7 +136,7 @@ exports.getScraping = function(req, res, next) {
/** /**
* GET /api/github * GET /api/github
* Show GitHub repository information * GitHub API Example.
*/ */
exports.getGithub = function(req, res) { exports.getGithub = function(req, res) {
var token = _.findWhere(req.user.tokens, { kind: 'github' }); var token = _.findWhere(req.user.tokens, { kind: 'github' });
@ -148,8 +153,9 @@ exports.getGithub = function(req, res) {
/** /**
* GET /api/aviary * GET /api/aviary
* Client-side Aviary image processing example * Aviary image processing example.
*/ */
exports.getAviary = function(req, res) { exports.getAviary = function(req, res) {
res.render('api/aviary', { res.render('api/aviary', {
title: 'Aviary API' title: 'Aviary API'
@ -158,8 +164,9 @@ exports.getAviary = function(req, res) {
/** /**
* GET /api/nyt * GET /api/nyt
* New York Times API example * New York Times API example.
*/ */
exports.getNewYorkTimes = function(req, res) { exports.getNewYorkTimes = function(req, res) {
var query = querystring.stringify({ 'api-key': secrets.nyt.key, 'list-name': 'young-adult' }); var query = querystring.stringify({ 'api-key': secrets.nyt.key, 'list-name': 'young-adult' });
var url = 'http://api.nytimes.com/svc/books/v2/lists?' + query; var url = 'http://api.nytimes.com/svc/books/v2/lists?' + query;
@ -174,9 +181,10 @@ exports.getNewYorkTimes = function(req, res) {
/** /**
* GET /api/lastfm * GET /api/lastfm
* Last.fm API example * Last.fm API example.
*/ */
exports.getLastfm = function(req, res) {
exports.getLastfm = function(req, res, next) {
var lastfm = new LastFmNode(secrets.lastfm); var lastfm = new LastFmNode(secrets.lastfm);
async.parallel({ async.parallel({
artistInfo: function(done) { artistInfo: function(done) {
@ -230,9 +238,10 @@ exports.getLastfm = function(req, res) {
/** /**
* GET /api/twitter * GET /api/twitter
* Twiter API example * Twiter API example.
*/ */
exports.getTwitter = function(req, res) {
exports.getTwitter = function(req, res, next) {
var token = _.findWhere(req.user.tokens, { kind: 'twitter' }); var token = _.findWhere(req.user.tokens, { kind: 'twitter' });
var T = new Twit({ var T = new Twit({
consumer_key: secrets.twitter.consumerKey, consumer_key: secrets.twitter.consumerKey,
@ -241,6 +250,7 @@ exports.getTwitter = function(req, res) {
access_token_secret: token.tokenSecret access_token_secret: token.tokenSecret
}); });
T.get('search/tweets', { q: 'hackathon since:2013-01-01', geocode: '40.71448,-74.00598,5mi', count: 50 }, function(err, reply) { T.get('search/tweets', { q: 'hackathon since:2013-01-01', geocode: '40.71448,-74.00598,5mi', count: 50 }, function(err, reply) {
if (err) return next(err);
res.render('api/twitter', { res.render('api/twitter', {
title: 'Twitter API', title: 'Twitter API',
tweets: reply.statuses tweets: reply.statuses

View File

@ -15,13 +15,17 @@ exports.getContact = function(req, res) {
/** /**
* POST /contact * POST /contact
* Send a contact form message via SendGrid. * Send a contact form via SendGrid.
* @param {string} email
* @param {string} name
* @param {string} message
*/ */
exports.postContact = function(req, res) { exports.postContact = function(req, res) {
var from = req.body.email; var from = req.body.email;
var name = req.body.name; var name = req.body.name;
var body = req.body.contactBody; var body = req.body.message;
var sendTo = 'sakhat@gmail.com'; var sendTo = 'sahat@me.com';
var subject = 'API Example | Contact Form'; var subject = 'API Example | Contact Form';
var email = new sendgrid.Email({ var email = new sendgrid.Email({

View File

@ -23,7 +23,7 @@ block content
.form-group .form-group
label(class='col-sm-2 control-label', for='contactBody') Body label(class='col-sm-2 control-label', for='contactBody') Body
.col-sm-8 .col-sm-8
textarea.form-control(type='text', name='contactBody', id='contactBody', rows='7') textarea.form-control(type='text', name='message', id='message', rows='7')
.form-group .form-group
.col-sm-offset-2.col-sm-8 .col-sm-offset-2.col-sm-8
button.btn.btn-success(type='submit') Send button.btn.btn-success(type='submit') Send