sahat / hackathon-starter

A boilerplate for Node.js web applications
MIT License
34.91k stars 8.18k forks source link

how to organize outgrown application routes in app.js #102

Closed john-qin closed 10 years ago

john-qin commented 10 years ago

my application routes section in app.js file just get bigger and bigger. I want to move them out to a separate page just for route. not sure how to do that.

/**

var homeController = require('./controllers/home'); var userController = require('./controllers/user'); //var apiController = require('./controllers/api'); var contactController = require('./controllers/contact'); var forgotController = require('./controllers/forgot'); var resetController = require('./controllers/reset'); var connectController = require('./controllers/connect'); var dropboxController = require('./controllers/dropbox');

/**

app.get('/', homeController.index); app.get('/login', userController.getLogin); app.post('/login', userController.postLogin); app.get('/logout', userController.logout); app.get('/forgot', forgotController.getForgot); app.post('/forgot', forgotController.postForgot); app.get('/reset/:token', resetController.getReset); app.post('/reset/:token', resetController.postReset); 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('/connect', connectController.index); app.get('/connect/dropbox', dropboxController.GetRequestToken); app.get('/auth/dropbox', dropboxController.GetAccessToken); app.get('/dropbox/list', dropboxController.List); app.get('/test', dropboxController.Test);

/**

app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] })); app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' })); app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' })); app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/', failureRedirect: '/login' }));

sahat commented 10 years ago

@john-qin This question should be asked on StackOverflow instead, since it's a general question about Express framework.

Replaces all your routes with

/**
 * Application routes.
 */

require('./routes.js')(app);

Then create a routes.js file in root directory that includes all your routes as well as controller references. Everything in routes.js should be wrapped with:

module.exports = function(app) {
  // routes go here
};

For example:

// routes.js
module.exports = function(app) {
  var homeController = require('./controllers/home');
  var userController = require('./controllers/user');
  var apiController = require('./controllers/api');
  var contactController = require('./controllers/contact');

  var passportConf = require('./config/passport');

  app.get('/', homeController.index);
  app.get('/login', userController.getLogin);
  app.post('/login', userController.postLogin);
  app.get('/logout', userController.logout);
  app.get('/forgot', userController.getForgot);
  app.post('/forgot', userController.postForgot);
  app.get('/reset/:token', userController.getReset);
  app.post('/reset/:token', userController.postReset);
  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/lastfm', apiController.getLastfm);
  app.get('/api/nyt', apiController.getNewYorkTimes);
  app.get('/api/aviary', apiController.getAviary);
  app.get('/api/paypal', apiController.getPayPal);
  app.get('/api/paypal/success', apiController.getPayPalSuccess);
  app.get('/api/paypal/cancel', apiController.getPayPalCancel);
  app.get('/api/steam', apiController.getSteam);
  app.get('/api/scraping', apiController.getScraping);
  app.get('/api/twilio', apiController.getTwilio);
  app.post('/api/twilio', apiController.postTwilio);
  app.get('/api/clockwork', apiController.getClockwork);
  app.post('/api/clockwork', apiController.postClockwork);
  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, passportConf.isAuthorized, apiController.getFacebook);
  app.get('/api/github', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getGithub);
  app.get('/api/twitter', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getTwitter);
  app.get('/api/venmo', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getVenmo);
  app.post('/api/venmo', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.postVenmo);
  app.get('/api/linkedin', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getLinkedin);

};
john-qin commented 10 years ago

really appreciate your help. i will try stackoverflow first , next time