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');
/**
* Controllers.
* Load controllers.
*/
var homeController = require('./controllers/home');
@ -21,19 +21,19 @@ var apiController = require('./controllers/api');
var contactController = require('./controllers/contact');
/**
* API keys and Passport configuration.
* API keys + Passport configuration.
*/
var secrets = require('./config/secrets');
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() {
console.log('MongoDB Connection Error');
console.log('MongoDB Connection Error');
});
var app = express();
@ -72,7 +72,7 @@ app.use(function(req, res) {
app.use(express.errorHandler());
/**
* Routes.
* Application routes.
*/
app.get('/', homeController.index);

View File

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

View File

@ -15,13 +15,17 @@ exports.getContact = function(req, res) {
/**
* 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) {
var from = req.body.email;
var name = req.body.name;
var body = req.body.contactBody;
var sendTo = 'sakhat@gmail.com';
var body = req.body.message;
var sendTo = 'sahat@me.com';
var subject = 'API Example | Contact Form';
var email = new sendgrid.Email({

View File

@ -23,7 +23,7 @@ block content
.form-group
label(class='col-sm-2 control-label', for='contactBody') Body
.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
.col-sm-offset-2.col-sm-8
button.btn.btn-success(type='submit') Send