Refactoring
This commit is contained in:
148
app.js
148
app.js
@ -26,104 +26,86 @@ var contactController = require('./controllers/contact');
|
||||
var secrets = require('./config/secrets');
|
||||
var passportConf = require('./config/passport');
|
||||
|
||||
mongoose.connect(secrets.db);
|
||||
|
||||
var app = express();
|
||||
|
||||
/**
|
||||
* Spawn worker processes.
|
||||
*/
|
||||
if (cluster.isMaster) {
|
||||
var numCPUs = require('os').cpus().length;
|
||||
|
||||
for (var i = 0; i < numCPUs; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
|
||||
cluster.on('disconnect', function(worker) {
|
||||
console.error('worker ' + worker.process.pid + ' died');
|
||||
cluster.fork();
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
mongoose.connect(secrets.db);
|
||||
|
||||
var app = express();
|
||||
|
||||
/**
|
||||
* Express configuration.
|
||||
*/
|
||||
app.set('port', process.env.PORT || 3000);
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
app.set('view engine', 'jade');
|
||||
app.use(express.favicon());
|
||||
app.use(express.logger('dev'));
|
||||
app.use(express.cookieParser());
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded());
|
||||
app.use(express.methodOverride());
|
||||
app.use(express.session({
|
||||
app.set('port', process.env.PORT || 3000);
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
app.set('view engine', 'jade');
|
||||
app.use(express.favicon());
|
||||
app.use(express.logger('dev'));
|
||||
app.use(express.cookieParser());
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded());
|
||||
app.use(express.methodOverride());
|
||||
app.use(express.session({
|
||||
secret: '0000',
|
||||
store: new MongoStore({ db: secrets.db })
|
||||
}));
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
app.use(function(req, res, next) {
|
||||
}));
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
app.use(function(req, res, next) {
|
||||
res.locals.user = req.user;
|
||||
next();
|
||||
});
|
||||
app.use(flash());
|
||||
app.use(less({ src: __dirname + '/public', compress: true }));
|
||||
app.use(app.router);
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(function(req, res) {
|
||||
});
|
||||
app.use(flash());
|
||||
app.use(less({ src: __dirname + '/public', compress: true }));
|
||||
app.use(app.router);
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(function(req, res) {
|
||||
res.status(404);
|
||||
res.render('404');
|
||||
});
|
||||
app.use(function(err, req, res, next){
|
||||
});
|
||||
app.use(function(err, req, res, next){
|
||||
console.error(err.stack);
|
||||
res.status(500);
|
||||
res.render('500');
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
/**
|
||||
* Routes.
|
||||
*/
|
||||
app.get('/', homeController.index);
|
||||
app.get('/login', userController.getLogin);
|
||||
app.post('/login', userController.postLogin);
|
||||
app.get('/logout', userController.logout);
|
||||
app.get('/signup', userController.getSignup);
|
||||
app.post('/signup', userController.postSignup);
|
||||
app.get('/contact', contactController.getContact);
|
||||
app.post('/contact', contactController.postContact);
|
||||
app.get('/account', passportConf.isAuthenticated, userController.getAccount);
|
||||
app.post('/account/profile', passportConf.isAuthenticated, userController.postUpdateProfile);
|
||||
app.post('/account/password', passportConf.isAuthenticated, userController.postUpdatePassword);
|
||||
app.post('/account/delete', passportConf.isAuthenticated, userController.postDeleteAccount);
|
||||
app.get('/account/unlink/:provider', passportConf.isAuthenticated, userController.getOauthUnlink);
|
||||
app.get('/api', apiController.getApi);
|
||||
app.get('/api/foursquare', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getFoursquare);
|
||||
app.get('/api/tumblr', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getTumblr);
|
||||
app.get('/api/facebook', passportConf.isAuthenticated, apiController.getFacebook);
|
||||
app.get('/api/scraping', apiController.getScraping);
|
||||
app.get('/api/github', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getGithub);
|
||||
app.get('/api/lastfm', apiController.getLastfm);
|
||||
app.get('/api/nyt', apiController.getNewYorkTimes);
|
||||
app.get('/api/twitter', passportConf.isAuthenticated, apiController.getTwitter);
|
||||
app.get('/api/aviary', apiController.getAviary);
|
||||
app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
|
||||
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
|
||||
app.get('/auth/github', passport.authenticate('github'));
|
||||
app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }));
|
||||
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
|
||||
app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/', failureRedirect: '/login' }));
|
||||
app.get('/auth/twitter', passport.authenticate('twitter'));
|
||||
app.get('/auth/twitter/callback', passport.authenticate('twitter', { successRedirect: '/', failureRedirect: '/login' }));
|
||||
app.get('/auth/foursquare', passport.authorize('foursquare'));
|
||||
app.get('/auth/foursquare/callback', passport.authorize('foursquare', { failureRedirect: '/api' }), function(req, res) { res.redirect('/api/foursquare'); });
|
||||
app.get('/auth/tumblr', passport.authorize('tumblr'));
|
||||
app.get('/auth/tumblr/callback', passport.authorize('tumblr', { failureRedirect: '/api' }), function(req, res) { res.redirect('/api/tumblr'); });
|
||||
app.get('/', homeController.index);
|
||||
app.get('/login', userController.getLogin);
|
||||
app.post('/login', userController.postLogin);
|
||||
app.get('/logout', userController.logout);
|
||||
app.get('/signup', userController.getSignup);
|
||||
app.post('/signup', userController.postSignup);
|
||||
app.get('/contact', contactController.getContact);
|
||||
app.post('/contact', contactController.postContact);
|
||||
app.get('/account', passportConf.isAuthenticated, userController.getAccount);
|
||||
app.post('/account/profile', passportConf.isAuthenticated, userController.postUpdateProfile);
|
||||
app.post('/account/password', passportConf.isAuthenticated, userController.postUpdatePassword);
|
||||
app.post('/account/delete', passportConf.isAuthenticated, userController.postDeleteAccount);
|
||||
app.get('/account/unlink/:provider', passportConf.isAuthenticated, userController.getOauthUnlink);
|
||||
app.get('/api', apiController.getApi);
|
||||
app.get('/api/foursquare', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getFoursquare);
|
||||
app.get('/api/tumblr', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getTumblr);
|
||||
app.get('/api/facebook', passportConf.isAuthenticated, apiController.getFacebook);
|
||||
app.get('/api/scraping', apiController.getScraping);
|
||||
app.get('/api/github', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getGithub);
|
||||
app.get('/api/lastfm', apiController.getLastfm);
|
||||
app.get('/api/nyt', apiController.getNewYorkTimes);
|
||||
app.get('/api/twitter', passportConf.isAuthenticated, apiController.getTwitter);
|
||||
app.get('/api/aviary', apiController.getAviary);
|
||||
app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
|
||||
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
|
||||
app.get('/auth/github', passport.authenticate('github'));
|
||||
app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }));
|
||||
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
|
||||
app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/', failureRedirect: '/login' }));
|
||||
app.get('/auth/twitter', passport.authenticate('twitter'));
|
||||
app.get('/auth/twitter/callback', passport.authenticate('twitter', { successRedirect: '/', failureRedirect: '/login' }));
|
||||
app.get('/auth/foursquare', passport.authorize('foursquare'));
|
||||
app.get('/auth/foursquare/callback', passport.authorize('foursquare', { failureRedirect: '/api' }), function(req, res) { res.redirect('/api/foursquare'); });
|
||||
app.get('/auth/tumblr', passport.authorize('tumblr'));
|
||||
app.get('/auth/tumblr/callback', passport.authorize('tumblr', { failureRedirect: '/api' }), function(req, res) { res.redirect('/api/tumblr'); });
|
||||
|
||||
app.listen(app.get('port'), function() {
|
||||
app.listen(app.get('port'), function() {
|
||||
console.log('Express server listening on port ' + app.get('port'));
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ var sendgrid = require('sendgrid')(secrets.sendgrid.user, secrets.sendgrid.pass
|
||||
|
||||
/**
|
||||
* GET /contact
|
||||
* Contact form page
|
||||
* Contact form page.
|
||||
*/
|
||||
exports.getContact = function(req, res) {
|
||||
res.render('contact', {
|
||||
@ -15,7 +15,7 @@ exports.getContact = function(req, res) {
|
||||
|
||||
/**
|
||||
* POST /contact
|
||||
* Send a contact form message via SendGrid
|
||||
* Send a contact form message via SendGrid.
|
||||
*/
|
||||
exports.postContact = function(req, res) {
|
||||
var from = req.body.email;
|
||||
|
@ -1,6 +1,6 @@
|
||||
/**
|
||||
* GET /
|
||||
* Home page
|
||||
* Home page.
|
||||
*/
|
||||
exports.index = function(req, res) {
|
||||
res.render('home', {
|
||||
|
@ -96,19 +96,13 @@ exports.postSignup = function(req, res, next) {
|
||||
password: req.body.password
|
||||
});
|
||||
|
||||
// TODO: simplify
|
||||
user.save(function(err) {
|
||||
if (err) {
|
||||
if (err.name === 'ValidationError') {
|
||||
// TODO: make more explicit
|
||||
req.flash('messages', _.map(err.errors, function(value, key) { return value.message; }));
|
||||
}
|
||||
if (err.code === 11000) {
|
||||
req.flash('messages', 'User already exists.');
|
||||
}
|
||||
return res.redirect('/signup');
|
||||
}
|
||||
|
||||
req.logIn(user, function(err) {
|
||||
if (err) return next(err);
|
||||
res.redirect('/');
|
||||
@ -133,7 +127,7 @@ exports.postUpdateProfile = function(req, res, next) {
|
||||
|
||||
user.save(function(err) {
|
||||
if (err) return next(err);
|
||||
req.flash('success', { success: 'Profile information updated' });
|
||||
req.flash('success', { success: 'Profile information updated.' });
|
||||
res.redirect('/account');
|
||||
});
|
||||
});
|
||||
@ -145,23 +139,24 @@ exports.postUpdateProfile = function(req, res, next) {
|
||||
*/
|
||||
exports.postUpdatePassword = function(req, res, next) {
|
||||
|
||||
// TODO: Use Virtuals (mongoose)
|
||||
if (!req.body.password || !req.body.confirmPassword) {
|
||||
req.flash('error', 'Passwords cannot be blank');
|
||||
if (!req.body.password) {
|
||||
req.flash('error', 'Passwords cannot be blank.');
|
||||
return res.redirect('/account');
|
||||
}
|
||||
|
||||
if (req.body.password !== req.body.confirmPassword) {
|
||||
req.flash('error', 'Passwords do not match');
|
||||
req.flash('error', 'Passwords do not match.');
|
||||
return res.redirect('/account');
|
||||
}
|
||||
|
||||
User.findById(req.user.id, function(err, user) {
|
||||
if (err) return next(err);
|
||||
|
||||
user.password = req.body.password;
|
||||
|
||||
user.save(function(err) {
|
||||
if (err) return next(err);
|
||||
req.flash('success', 'Password has been changed');
|
||||
req.flash('success', 'Password has been changed.');
|
||||
res.redirect('/account');
|
||||
});
|
||||
});
|
||||
@ -181,7 +176,7 @@ exports.postDeleteAccount = function(req, res, next) {
|
||||
|
||||
/**
|
||||
* GET /account/unlink/:provider
|
||||
* Unlink an oauth provider from the current user
|
||||
* Unlink OAuth2 provider from the current user.
|
||||
*/
|
||||
exports.getOauthUnlink = function(req, res, next) {
|
||||
var provider = req.params.provider;
|
||||
@ -200,7 +195,7 @@ exports.getOauthUnlink = function(req, res, next) {
|
||||
|
||||
/**
|
||||
* GET /logout
|
||||
* Log out
|
||||
* Log out.
|
||||
*/
|
||||
exports.logout = function(req, res) {
|
||||
req.logout();
|
||||
|
Reference in New Issue
Block a user