2014-12-22 16:16:10 -08:00
require ( 'dotenv' ) . load ( ) ;
2015-03-22 22:18:01 -07:00
// handle uncaught exceptions. Forever will restart process on shutdown
process . on ( 'uncaughtException' , function ( err ) {
console . error (
( new Date ( ) ) . toUTCString ( ) + ' uncaughtException:' ,
err . message
) ;
console . error ( err . stack ) ;
/* eslint-disable no-process-exit */
process . exit ( 1 ) ;
/* eslint-enable no-process-exit */
} ) ;
2014-01-11 22:53:31 -05:00
2014-12-23 08:48:28 -08:00
var express = require ( 'express' ) ,
2015-04-14 15:36:02 -04:00
accepts = require ( 'accepts' ) ,
cookieParser = require ( 'cookie-parser' ) ,
compress = require ( 'compression' ) ,
session = require ( 'express-session' ) ,
logger = require ( 'morgan' ) ,
errorHandler = require ( 'errorhandler' ) ,
methodOverride = require ( 'method-override' ) ,
bodyParser = require ( 'body-parser' ) ,
helmet = require ( 'helmet' ) ,
MongoStore = require ( 'connect-mongo' ) ( session ) ,
flash = require ( 'express-flash' ) ,
path = require ( 'path' ) ,
mongoose = require ( 'mongoose' ) ,
passport = require ( 'passport' ) ,
expressValidator = require ( 'express-validator' ) ,
request = require ( 'request' ) ,
2015-05-01 16:34:07 -04:00
forceDomain = require ( 'forcedomain' ) ,
2015-05-03 16:45:34 -07:00
lessMiddleware = require ( 'less-middleware' ) ,
2014-01-11 22:53:31 -05:00
2014-12-23 08:48:28 -08:00
/ * *
2015-01-16 18:58:27 -05:00
* Controllers ( route handlers ) .
* /
2014-12-23 08:48:28 -08:00
homeController = require ( './controllers/home' ) ,
resourcesController = require ( './controllers/resources' ) ,
userController = require ( './controllers/user' ) ,
2015-03-24 19:08:26 -07:00
nonprofitController = require ( './controllers/nonprofits' ) ,
2015-01-11 00:45:22 -05:00
bonfireController = require ( './controllers/bonfire' ) ,
2015-02-01 23:35:27 -08:00
coursewareController = require ( './controllers/courseware' ) ,
2015-04-08 17:18:51 -07:00
fieldGuideController = require ( './controllers/fieldGuide' ) ,
2015-04-03 20:05:53 -07:00
challengeMapController = require ( './controllers/challengeMap' ) ,
2013-11-14 02:29:55 -05:00
2015-04-17 00:11:13 -04:00
/ * *
2015-03-03 19:23:56 +09:00
* Stories
2015-01-16 18:58:27 -05:00
* /
2015-04-29 19:30:51 -04:00
storyController = require ( './controllers/story' ) ,
2014-01-11 22:53:31 -05:00
2014-12-23 08:48:28 -08:00
/ * *
2015-01-16 18:58:27 -05:00
* API keys and Passport configuration .
* /
2014-12-23 08:48:28 -08:00
secrets = require ( './config/secrets' ) ,
passportConf = require ( './config/passport' ) ;
2013-11-26 23:15:13 -05:00
2014-02-02 05:38:38 -05:00
/ * *
* Create Express server .
* /
var app = express ( ) ;
2014-01-11 22:53:31 -05:00
/ * *
2014-06-06 14:58:30 -04:00
* Connect to MongoDB .
2014-01-11 22:53:31 -05:00
* /
2014-02-25 22:39:28 -05:00
mongoose . connect ( secrets . db ) ;
2015-01-16 18:58:27 -05:00
mongoose . connection . on ( 'error' , function ( ) {
console . error (
'MongoDB Connection Error. Please make sure that MongoDB is running.'
) ;
2014-01-11 22:53:31 -05:00
} ) ;
2013-11-13 12:32:22 -05:00
2014-05-06 00:44:30 -04:00
/ * *
* Express configuration .
* /
2014-04-18 14:29:30 -04:00
2015-02-17 15:35:16 -08:00
2014-01-11 22:53:31 -05:00
app . set ( 'port' , process . env . PORT || 3000 ) ;
app . set ( 'views' , path . join ( _ _dirname , 'views' ) ) ;
app . set ( 'view engine' , 'jade' ) ;
2015-04-22 14:53:58 -07:00
if ( process . env . NODE _ENV === 'production' ) {
2015-05-01 16:34:07 -04:00
app . use ( forceDomain ( {
hostname : 'www.freecodecamp.com'
} ) ) ;
2015-04-22 14:53:58 -07:00
}
2014-06-06 14:58:30 -04:00
app . use ( compress ( ) ) ;
2015-05-03 16:45:34 -07:00
app . use ( lessMiddleware ( _ _dirname + '/public' ) ) ;
2014-04-12 12:43:07 -04:00
app . use ( logger ( 'dev' ) ) ;
app . use ( bodyParser . json ( ) ) ;
2015-01-16 18:58:27 -05:00
app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
2014-12-23 18:20:53 -08:00
app . use ( expressValidator ( {
customValidators : {
2015-01-16 18:58:27 -05:00
matchRegex : function ( param , regex ) {
2014-12-23 18:20:53 -08:00
return regex . test ( param ) ;
}
}
} ) ) ;
2014-04-12 12:43:07 -04:00
app . use ( methodOverride ( ) ) ;
app . use ( cookieParser ( ) ) ;
app . use ( session ( {
2015-04-14 15:36:02 -04:00
resave : true ,
saveUninitialized : true ,
secret : secrets . sessionSecret ,
store : new MongoStore ( {
url : secrets . db ,
2015-04-16 23:16:55 -07:00
'autoReconnect' : true
2015-04-14 15:36:02 -04:00
} )
2014-01-29 00:49:09 -05:00
} ) ) ;
2014-01-11 22:53:31 -05:00
app . use ( passport . initialize ( ) ) ;
app . use ( passport . session ( ) ) ;
2014-06-01 11:52:28 -04:00
app . use ( flash ( ) ) ;
2014-12-10 20:44:33 -08:00
app . disable ( 'x-powered-by' ) ;
2014-12-23 08:48:28 -08:00
2014-12-10 20:44:33 -08:00
app . use ( helmet . xssFilter ( ) ) ;
2015-01-09 15:10:34 -08:00
app . use ( helmet . noSniff ( ) ) ;
2014-12-10 20:44:33 -08:00
app . use ( helmet . xframe ( ) ) ;
2015-02-17 15:35:16 -08:00
app . use ( function ( req , res , next ) {
2015-04-29 19:30:51 -04:00
res . header ( 'Access-Control-Allow-Origin' , '*' ) ;
res . header ( 'Access-Control-Allow-Headers' ,
'Origin, X-Requested-With, Content-Type, Accept'
) ;
2015-02-17 15:35:16 -08:00
next ( ) ;
} ) ;
2014-12-23 08:48:28 -08:00
2014-12-22 12:36:45 -08:00
var trusted = [
2015-04-14 15:36:02 -04:00
"'self'" ,
2015-05-06 18:14:00 -04:00
'blob:' ,
2015-04-14 15:36:02 -04:00
'*.freecodecamp.com' ,
'*.gstatic.com' ,
'*.google-analytics.com' ,
'*.googleapis.com' ,
'*.google.com' ,
'*.gstatic.com' ,
'*.doubleclick.net' ,
'*.twitter.com' ,
'*.twitch.tv' ,
'*.twimg.com' ,
"'unsafe-eval'" ,
"'unsafe-inline'" ,
'*.bootstrapcdn.com' ,
'*.cloudflare.com' ,
'https://*.cloudflare.com' ,
'localhost:3001' ,
'ws://localhost:3001/' ,
'http://localhost:3001' ,
'localhost:3000' ,
'ws://localhost:3000/' ,
'http://localhost:3000' ,
'*.ionicframework.com' ,
'https://syndication.twitter.com' ,
'*.youtube.com' ,
'*.jsdelivr.net' ,
'https://*.jsdelivr.net' ,
'*.ytimg.com' ,
2015-04-14 23:04:54 -04:00
'*.bitly.com' ,
'http://cdn.inspectlet.com/' ,
'http://hn.inspectlet.com/'
2014-12-22 12:36:45 -08:00
] ;
2014-12-22 13:38:48 -08:00
2014-12-10 20:44:33 -08:00
app . use ( helmet . contentSecurityPolicy ( {
2014-12-22 12:36:45 -08:00
defaultSrc : trusted ,
2015-02-19 17:18:44 -08:00
scriptSrc : [
'*.optimizely.com' ,
'*.aspnetcdn.com' ,
2015-05-06 18:14:00 -04:00
'*.d3js.org'
2015-02-19 17:18:44 -08:00
] . concat ( trusted ) ,
2014-12-23 13:50:14 -08:00
'connect-src' : [
2015-05-06 18:14:00 -04:00
'ws://www.freecodecamp.com'
2015-01-09 07:53:29 -08:00
] . concat ( trusted ) ,
2014-12-22 12:36:45 -08:00
styleSrc : trusted ,
2014-12-23 13:50:14 -08:00
imgSrc : [
2015-05-06 18:14:00 -04:00
/* allow all input since we have user submitted images for public profile*/
2015-04-29 19:30:51 -04:00
'*'
2014-12-23 13:50:14 -08:00
] . concat ( trusted ) ,
fontSrc : [ '*.googleapis.com' ] . concat ( trusted ) ,
mediaSrc : [
2015-01-16 18:58:27 -05:00
'*.amazonaws.com' ,
'*.twitter.com'
2015-01-09 07:53:29 -08:00
] . concat ( trusted ) ,
2014-12-23 13:50:14 -08:00
frameSrc : [
2015-01-16 18:58:27 -05:00
'*.gitter.im' ,
2015-03-10 17:42:15 -05:00
'*.gitter.im https:' ,
2015-01-16 18:58:27 -05:00
'*.vimeo.com' ,
'*.twitter.com' ,
2015-02-15 23:59:03 -08:00
'*.ghbtns.com'
2015-01-09 07:53:29 -08:00
] . concat ( trusted ) ,
2014-12-10 20:44:33 -08:00
reportOnly : false , // set to true if you only want to report errors
setAllHeaders : false , // set to true if you want to set all headers
safari5 : false // set to true if you want to force buggy CSP in Safari 5
} ) ) ;
2014-11-19 15:30:36 -08:00
2015-01-16 18:58:27 -05:00
app . use ( function ( req , res , next ) {
2015-04-14 15:36:02 -04:00
// Make user object available in templates.
res . locals . user = req . user ;
next ( ) ;
2014-01-11 22:53:31 -05:00
} ) ;
2014-11-19 15:30:36 -08:00
2015-05-06 09:10:15 -04:00
app . use ( express . static ( _ _dirname + '/public' , { maxAge : 86400000 } ) ) ;
2015-01-16 18:58:27 -05:00
app . use ( function ( req , res , next ) {
2014-10-17 19:23:53 -07:00
// Remember original destination before login.
var path = req . path . split ( '/' ) [ 1 ] ;
2015-03-28 00:16:48 -05:00
if ( /auth|login|logout|signin|signup|fonts|favicon/i . test ( path ) ) {
return next ( ) ;
} else if ( /\/stories\/comments\/\w+/i . test ( req . path ) ) {
2014-10-17 19:23:53 -07:00
return next ( ) ;
}
req . session . returnTo = req . path ;
next ( ) ;
2014-03-08 14:58:27 -05:00
} ) ;
2014-11-19 15:30:36 -08:00
2014-01-11 22:53:31 -05:00
/ * *
2014-06-06 15:23:28 -04:00
* Main routes .
2014-01-11 22:53:31 -05:00
* /
2014-11-19 15:30:36 -08:00
2015-01-14 13:28:20 -08:00
app . get ( '/' , homeController . index ) ;
2015-03-30 15:15:07 -07:00
app . get ( '/nonprofit-project-instructions' , function ( req , res ) {
2015-04-24 13:49:32 -04:00
res . redirect ( 301 , '/field-guide/nonprofit-project-instructions' ) ;
2015-03-30 15:15:07 -07:00
} ) ;
2015-02-19 16:33:08 -08:00
app . get ( '/chat' , resourcesController . chat ) ;
2015-03-30 15:15:07 -07:00
2015-04-07 00:11:31 -07:00
app . get ( '/twitch' , resourcesController . twitch ) ;
2015-04-27 16:17:29 -07:00
// Agile Project Manager Onboarding
2015-04-29 19:30:51 -04:00
app . get ( '/pmi-acp-agile-project-managers' ,
resourcesController . agileProjectManagers ) ;
2015-04-27 16:17:29 -07:00
app . get ( '/agile' , function ( req , res ) {
res . redirect ( 301 , '/pmi-acp-agile-project-managers' ) ;
} ) ;
2015-04-29 19:30:51 -04:00
app . get ( '/pmi-acp-agile-project-managers-form' ,
resourcesController . agileProjectManagersForm ) ;
2015-04-27 16:17:29 -07:00
// Nonprofit Onboarding
app . get ( '/nonprofits' , resourcesController . nonprofits ) ;
2015-05-01 22:22:10 -07:00
app . get ( '/nonprofits/getNonprofitList' , nonprofitController . showAllNonprofits ) ;
2015-04-27 16:17:29 -07:00
2015-05-01 22:22:10 -07:00
app . get ( '/nonprofits-form' , resourcesController . nonprofitsForm ) ;
2015-04-27 16:17:29 -07:00
2015-04-05 18:37:58 -07:00
app . get ( '/map' , challengeMapController . challengeMap ) ;
2015-04-03 20:05:53 -07:00
2015-03-30 15:15:07 -07:00
app . get ( '/live-pair-programming' , function ( req , res ) {
2015-04-08 17:18:51 -07:00
res . redirect ( 301 , '/field-guide/live-stream-pair-programming-on-twitch.tv' ) ;
2015-03-30 15:15:07 -07:00
} ) ;
app . get ( '/install-screenhero' , function ( req , res ) {
2015-04-08 17:18:51 -07:00
res . redirect ( 301 , '/field-guide/install-screenhero' ) ;
2015-03-30 15:15:07 -07:00
} ) ;
app . get ( '/guide-to-our-nonprofit-projects' , function ( req , res ) {
2015-04-08 17:18:51 -07:00
res . redirect ( 301 , '/field-guide/a-guide-to-our-nonprofit-projects' ) ;
2015-03-30 15:15:07 -07:00
} ) ;
app . get ( '/chromebook' , function ( req , res ) {
2015-04-08 17:18:51 -07:00
res . redirect ( 301 , '/field-guide/chromebook' ) ;
2015-03-30 15:15:07 -07:00
} ) ;
app . get ( '/deploy-a-website' , function ( req , res ) {
2015-04-08 17:18:51 -07:00
res . redirect ( 301 , '/field-guide/deploy-a-website' ) ;
2015-03-30 15:15:07 -07:00
} ) ;
app . get ( '/gmail-shortcuts' , function ( req , res ) {
2015-04-08 17:18:51 -07:00
res . redirect ( 301 , '/field-guide/gmail-shortcuts' ) ;
2015-03-30 15:15:07 -07:00
} ) ;
app . get ( '/nodeschool-challenges' , function ( req , res ) {
2015-04-08 17:18:51 -07:00
res . redirect ( 301 , '/field-guide/nodeschool-challenges' ) ;
2015-03-30 15:15:07 -07:00
} ) ;
2015-04-23 23:46:58 -07:00
app . get ( '/learn-to-code' , challengeMapController . challengeMap ) ;
2015-01-26 11:38:19 -08:00
app . get ( '/about' , function ( req , res ) {
2015-04-23 23:46:58 -07:00
res . redirect ( 301 , '/map' ) ;
2015-03-30 15:15:07 -07:00
} ) ;
2015-01-26 11:38:19 -08:00
app . get ( '/signin' , userController . getSignin ) ;
2015-03-30 15:15:07 -07:00
2015-01-26 11:38:19 -08:00
app . get ( '/login' , function ( req , res ) {
2015-04-14 15:36:02 -04:00
res . redirect ( 301 , '/signin' ) ;
2015-01-26 11:38:19 -08:00
} ) ;
2015-03-30 15:15:07 -07:00
2015-01-26 11:38:19 -08:00
app . post ( '/signin' , userController . postSignin ) ;
2015-03-30 15:15:07 -07:00
2015-01-26 11:38:19 -08:00
app . get ( '/signout' , userController . signout ) ;
2015-03-30 15:15:07 -07:00
2015-01-26 11:38:19 -08:00
app . get ( '/logout' , function ( req , res ) {
2015-04-14 15:36:02 -04:00
res . redirect ( 301 , '/signout' ) ;
2015-01-26 11:38:19 -08:00
} ) ;
2015-03-30 15:15:07 -07:00
2014-06-06 15:23:28 -04:00
app . get ( '/forgot' , userController . getForgot ) ;
2015-03-30 15:15:07 -07:00
2014-06-06 15:23:28 -04:00
app . post ( '/forgot' , userController . postForgot ) ;
2015-03-30 15:15:07 -07:00
2014-06-06 15:23:28 -04:00
app . get ( '/reset/:token' , userController . getReset ) ;
2015-03-30 15:15:07 -07:00
2014-06-06 15:23:28 -04:00
app . post ( '/reset/:token' , userController . postReset ) ;
2015-03-30 15:15:07 -07:00
2014-12-07 16:25:43 -08:00
app . get ( '/email-signup' , userController . getEmailSignup ) ;
2015-03-30 15:15:07 -07:00
2014-12-07 16:25:43 -08:00
app . get ( '/email-signin' , userController . getEmailSignin ) ;
2015-03-30 15:15:07 -07:00
2014-12-07 16:25:43 -08:00
app . post ( '/email-signup' , userController . postEmailSignup ) ;
2015-03-30 15:15:07 -07:00
2015-01-26 11:38:19 -08:00
app . post ( '/email-signin' , userController . postSignin ) ;
2015-03-30 15:15:07 -07:00
/ * *
* Nonprofit Project routes .
* /
2015-04-08 14:36:05 -07:00
app . get ( '/nonprofits/directory' , nonprofitController . nonprofitsDirectory ) ;
2015-03-30 15:15:07 -07:00
2015-03-30 18:02:50 -07:00
app . get (
'/nonprofits/:nonprofitName' ,
nonprofitController . returnIndividualNonprofit
) ;
2014-11-19 15:50:57 -08:00
app . post (
2015-04-14 15:36:02 -04:00
'/update-progress' ,
passportConf . isAuthenticated ,
userController . updateProgress
2014-12-23 08:48:28 -08:00
) ;
2015-02-15 23:59:03 -08:00
2015-04-24 13:49:32 -04:00
app . get ( '/privacy' , function ( req , res ) {
2015-05-05 18:31:03 -07:00
res . redirect ( 301 , '/field-guide/what-is-free-code-camp\'s-privacy-policy' ) ;
2015-04-24 13:49:32 -04:00
} ) ;
2015-04-08 22:42:40 -07:00
app . get ( '/api/slack' , function ( req , res ) {
if ( req . user ) {
if ( req . user . email ) {
var invite = {
'email' : req . user . email ,
'token' : process . env . SLACK _KEY ,
'set_active' : true
} ;
var headers = {
'User-Agent' : 'Node Browser/0.0.1' ,
'Content-Type' : 'application/x-www-form-urlencoded'
} ;
var options = {
url : 'https://freecode.slack.com/api/users.admin.invite' ,
method : 'POST' ,
headers : headers ,
form : invite
} ;
request ( options , function ( error , response , body ) {
if ( ! error && response . statusCode === 200 ) {
req . flash ( 'success' , {
msg : "We've successfully requested an invite for you. Please check your email and follow the instructions from Slack."
} ) ;
req . user . sentSlackInvite = true ;
req . user . save ( function ( err , user ) {
if ( err ) {
next ( err ) ;
}
return res . redirect ( 'back' ) ;
} ) ;
} else {
req . flash ( 'errors' , {
msg : "The invitation email did not go through for some reason. Please try again or <a href='mailto:team@freecodecamp.com?subject=slack%20invite%20failed%20to%20send>email us</a>."
} ) ;
return res . redirect ( 'back' ) ;
}
2015-04-29 20:55:47 -04:00
} ) ;
2015-04-08 22:42:40 -07:00
} else {
req . flash ( 'notice' , {
msg : "Before we can send your Slack invite, we need your email address. Please update your profile information here."
} ) ;
return res . redirect ( '/account' ) ;
}
} else {
req . flash ( 'notice' , {
msg : "You need to sign in to Free Code Camp before we can send you a Slack invite."
} ) ;
return res . redirect ( '/account' ) ;
}
} ) ;
2015-02-15 23:59:03 -08:00
2015-03-03 19:23:56 +09:00
/ * *
2015-03-30 15:15:07 -07:00
* Camper News routes .
2015-03-03 19:23:56 +09:00
* /
2015-03-06 08:11:18 +09:00
app . get (
2015-04-14 15:36:02 -04:00
'/stories/hotStories' ,
storyController . hotJSON
2015-03-06 08:11:18 +09:00
) ;
app . get (
2015-04-14 15:36:02 -04:00
'/stories/recentStories' ,
storyController . recentJSON
2015-03-06 08:11:18 +09:00
) ;
2015-03-03 19:23:56 +09:00
app . get (
2015-04-14 15:36:02 -04:00
'/stories/comments/:id' ,
storyController . comments
2015-03-03 19:23:56 +09:00
) ;
2015-03-04 07:15:00 +09:00
2015-03-06 06:08:40 +09:00
app . post (
2015-04-14 15:36:02 -04:00
'/stories/comment/' ,
storyController . commentSubmit
2015-03-06 06:08:40 +09:00
) ;
2015-03-07 01:57:09 +09:00
app . post (
2015-04-14 15:36:02 -04:00
'/stories/comment/:id/comment' ,
storyController . commentOnCommentSubmit
2015-03-07 01:57:09 +09:00
) ;
2015-04-18 11:53:46 -04:00
app . put (
2015-04-18 02:40:48 -04:00
'/stories/comment/:id/edit' ,
storyController . commentEdit
) ;
2015-03-04 07:15:00 +09:00
app . get (
2015-04-14 15:36:02 -04:00
'/stories/submit' ,
storyController . submitNew
2015-03-04 07:15:00 +09:00
) ;
2015-03-07 17:42:22 +09:00
app . get (
2015-04-14 15:36:02 -04:00
'/stories/submit/new-story' ,
storyController . preSubmit
2015-03-05 19:21:26 +09:00
) ;
2015-03-07 17:42:22 +09:00
app . post (
2015-04-14 15:36:02 -04:00
'/stories/preliminary' ,
storyController . newStory
2015-03-07 17:42:22 +09:00
) ;
2015-03-06 08:11:18 +09:00
2015-03-07 18:26:49 +09:00
app . post (
2015-04-14 15:36:02 -04:00
'/stories/' ,
storyController . storySubmission
2015-03-07 18:26:49 +09:00
) ;
2015-03-06 08:11:18 +09:00
app . get (
2015-04-30 15:56:14 -07:00
'/news/' ,
2015-04-14 15:36:02 -04:00
storyController . hot
2015-03-06 08:11:18 +09:00
) ;
2015-03-06 09:20:30 +09:00
app . post (
2015-04-14 15:36:02 -04:00
'/stories/search' ,
storyController . getStories
2015-03-06 09:20:30 +09:00
) ;
2015-03-03 19:50:16 +09:00
app . get (
2015-04-30 15:56:14 -07:00
'/news/:storyName' ,
2015-04-14 15:36:02 -04:00
storyController . returnIndividualStory
2015-03-03 19:50:16 +09:00
) ;
2015-03-06 08:11:18 +09:00
2015-03-03 22:03:33 +09:00
app . post (
2015-04-14 15:36:02 -04:00
'/stories/upvote/' ,
storyController . upvote
2015-03-03 22:03:33 +09:00
) ;
2015-03-03 19:23:56 +09:00
2015-04-30 23:41:40 -07:00
app . get (
'/unsubscribe/:email' ,
resourcesController . unsubscribe
) ;
app . get (
'/unsubscribed' ,
resourcesController . unsubscribed
) ;
2014-11-19 15:30:36 -08:00
app . all ( '/account' , passportConf . isAuthenticated ) ;
2015-03-30 15:15:07 -07:00
2015-01-06 00:52:30 -05:00
app . get ( '/account/api' , userController . getAccountAngular ) ;
2015-01-27 20:12:51 -05:00
2015-02-16 23:35:02 -08:00
/ * *
* API routes
* /
app . get ( '/api/github' , resourcesController . githubCalls ) ;
2015-03-30 15:15:07 -07:00
2015-03-27 23:30:06 -07:00
app . get ( '/api/blogger' , resourcesController . bloggerCalls ) ;
2015-03-30 15:15:07 -07:00
2015-03-27 23:30:06 -07:00
app . get ( '/api/trello' , resourcesController . trelloCalls ) ;
2015-02-16 23:35:02 -08:00
2015-04-29 20:07:38 -04:00
app . get ( '/api/codepen/twitter/:screenName' , resourcesController . codepenResources . twitter ) ;
2015-04-29 08:00:57 -04:00
2015-01-27 20:12:51 -05:00
/ * *
* Bonfire related routes
* /
2015-02-22 17:36:43 +09:00
2015-04-08 17:18:51 -07:00
app . get ( '/field-guide/getFieldGuideList' , fieldGuideController . showAllFieldGuides ) ;
2015-03-30 15:55:00 -07:00
2015-01-24 14:42:34 -08:00
app . get ( '/playground' , bonfireController . index ) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . get ( '/bonfires' , bonfireController . returnNextBonfire ) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . get ( '/bonfire-json-generator' , bonfireController . returnGenerator ) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . post ( '/bonfire-json-generator' , bonfireController . generateChallenge ) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . get ( '/bonfire-challenge-generator' , bonfireController . publicGenerator ) ;
2015-03-30 15:15:07 -07:00
2015-03-30 15:55:00 -07:00
app . post ( '/bonfire-challenge-generator' , bonfireController . testBonfire ) ;
2015-03-30 15:15:07 -07:00
2015-01-22 13:20:46 -05:00
app . get (
2015-04-14 15:36:02 -04:00
'/bonfires/:bonfireName' ,
bonfireController . returnIndividualBonfire
2015-01-22 13:20:46 -05:00
) ;
2015-03-30 15:15:07 -07:00
2015-01-26 12:30:04 -05:00
app . get ( '/bonfire' , function ( req , res ) {
2015-04-14 15:36:02 -04:00
res . redirect ( 301 , '/playground' ) ;
2015-01-26 12:30:04 -05:00
} ) ;
2014-02-01 03:30:14 -05:00
2015-03-30 18:02:50 -07:00
app . post ( '/completed-bonfire/' , bonfireController . completedBonfire ) ;
2015-03-30 13:48:54 -07:00
/ * *
2015-04-08 17:18:51 -07:00
* Field Guide related routes
2015-03-30 13:48:54 -07:00
* /
2015-02-22 16:27:38 +09:00
2015-03-30 13:48:54 -07:00
2015-04-29 19:30:51 -04:00
app . get ( '/field-guide/:fieldGuideName' ,
fieldGuideController . returnIndividualFieldGuide
) ;
2015-04-05 16:44:07 -07:00
2015-04-24 13:49:32 -04:00
app . get ( '/field-guide/' , fieldGuideController . returnNextFieldGuide ) ;
2015-04-05 16:44:07 -07:00
2015-04-08 17:18:51 -07:00
app . post ( '/completed-field-guide/' , fieldGuideController . completedFieldGuide ) ;
2015-02-22 16:27:38 +09:00
2015-01-24 00:44:08 -05:00
2015-02-01 23:35:27 -08:00
/ * *
* Courseware related routes
* /
2015-03-21 18:42:19 +09:00
app . get ( '/challenges/' , coursewareController . returnNextCourseware ) ;
2015-03-30 15:15:07 -07:00
2015-02-01 23:35:27 -08:00
app . get (
2015-03-21 18:42:19 +09:00
'/challenges/:coursewareName' ,
2015-02-01 23:35:27 -08:00
coursewareController . returnIndividualCourseware
) ;
2015-03-30 15:15:07 -07:00
2015-02-01 23:35:27 -08:00
app . post ( '/completed-courseware/' , coursewareController . completedCourseware ) ;
2015-03-30 15:15:07 -07:00
2015-03-29 20:39:41 +09:00
app . post ( '/completed-zipline-or-basejump' ,
coursewareController . completedZiplineOrBasejump ) ;
2015-02-01 23:35:27 -08:00
2015-01-27 20:12:51 -05:00
// Unique Check API route
2015-04-29 19:30:51 -04:00
app . get ( '/api/checkUniqueUsername/:username' ,
userController . checkUniqueUsername
) ;
2015-03-30 15:15:07 -07:00
2015-04-29 19:30:51 -04:00
app . get ( '/api/checkExistingUsername/:username' ,
userController . checkExistingUsername
) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . get ( '/api/checkUniqueEmail/:email' , userController . checkUniqueEmail ) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . get ( '/account' , userController . getAccount ) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . post ( '/account/profile' , userController . postUpdateProfile ) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . post ( '/account/password' , userController . postUpdatePassword ) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . post ( '/account/delete' , userController . postDeleteAccount ) ;
2015-03-30 15:15:07 -07:00
2015-01-27 20:12:51 -05:00
app . get ( '/account/unlink/:provider' , userController . getOauthUnlink ) ;
2015-03-30 15:15:07 -07:00
2015-03-06 00:20:39 -08:00
app . get ( '/sitemap.xml' , resourcesController . sitemap ) ;
2015-03-30 18:02:50 -07:00
2014-02-01 03:30:14 -05:00
/ * *
2014-06-06 14:58:30 -04:00
* OAuth sign - in routes .
2014-02-01 03:30:14 -05:00
* /
2014-12-23 13:50:14 -08:00
var passportOptions = {
2015-04-14 15:36:02 -04:00
successRedirect : '/' ,
failureRedirect : '/login'
2014-12-23 13:50:14 -08:00
} ;
2015-01-17 18:52:58 -08:00
app . get ( '/auth/twitter' , passport . authenticate ( 'twitter' ) ) ;
2015-03-30 15:15:07 -07:00
2014-11-19 15:30:36 -08:00
app . get (
2015-04-14 15:36:02 -04:00
'/auth/twitter/callback' ,
passport . authenticate ( 'twitter' , {
successRedirect : '/' ,
failureRedirect : '/login'
} )
2014-12-23 13:50:14 -08:00
) ;
2014-11-19 15:30:36 -08:00
app . get (
2015-04-14 15:36:02 -04:00
'/auth/linkedin' ,
passport . authenticate ( 'linkedin' , {
state : 'SOME STATE'
} )
2014-12-23 13:50:14 -08:00
) ;
2014-11-19 15:30:36 -08:00
app . get (
2015-04-14 15:36:02 -04:00
'/auth/linkedin/callback' ,
passport . authenticate ( 'linkedin' , passportOptions )
2014-12-23 13:50:14 -08:00
) ;
2014-11-19 15:30:36 -08:00
2014-12-23 08:48:28 -08:00
app . get (
2015-04-14 15:36:02 -04:00
'/auth/facebook' ,
passport . authenticate ( 'facebook' , { scope : [ 'email' , 'user_location' ] } )
2014-12-23 08:48:28 -08:00
) ;
app . get (
2015-04-14 15:36:02 -04:00
'/auth/facebook/callback' ,
passport . authenticate ( 'facebook' , passportOptions ) , function ( req , res ) {
res . redirect ( req . session . returnTo || '/' ) ;
}
2014-12-23 08:48:28 -08:00
) ;
2014-11-29 22:22:27 -08:00
app . get ( '/auth/github' , passport . authenticate ( 'github' ) ) ;
2015-03-30 15:15:07 -07:00
2014-12-23 08:48:28 -08:00
app . get (
2015-04-14 15:36:02 -04:00
'/auth/github/callback' ,
passport . authenticate ( 'github' , passportOptions ) , function ( req , res ) {
res . redirect ( req . session . returnTo || '/' ) ;
}
2014-12-23 08:48:28 -08:00
) ;
2014-11-29 22:22:27 -08:00
2014-12-23 08:48:28 -08:00
app . get (
2015-04-14 15:36:02 -04:00
'/auth/google' ,
passport . authenticate ( 'google' , { scope : 'profile email' } )
2014-12-23 08:48:28 -08:00
) ;
2015-03-30 15:15:07 -07:00
2014-12-23 08:48:28 -08:00
app . get (
2015-04-14 15:36:02 -04:00
'/auth/google/callback' ,
passport . authenticate ( 'google' , passportOptions ) , function ( req , res ) {
res . redirect ( req . session . returnTo || '/' ) ;
}
2014-12-23 08:48:28 -08:00
) ;
2014-11-29 22:22:27 -08:00
2015-03-24 08:03:59 -07:00
// put this route last
2015-01-17 18:52:58 -08:00
app . get (
2015-04-14 15:36:02 -04:00
'/:username' ,
userController . returnUser
2015-01-17 18:52:58 -08:00
) ;
2015-01-11 00:45:22 -05:00
2014-11-19 15:30:36 -08:00
/ * *
* 500 Error Handler .
* /
2015-03-24 08:03:59 -07:00
if ( process . env . NODE _ENV === 'development' ) {
app . use ( errorHandler ( { log : true } ) ) ;
} else {
// error handling in production
app . use ( function ( err , req , res , next ) {
// respect err.status
if ( err . status ) {
res . statusCode = err . status ;
}
// default status code to 500
if ( res . statusCode < 400 ) {
res . statusCode = 500 ;
}
// parse res type
var accept = accepts ( req ) ;
var type = accept . type ( 'html' , 'json' , 'text' ) ;
var message = 'opps! Something went wrong. Please try again later' ;
if ( type === 'html' ) {
req . flash ( 'errors' , { msg : message } ) ;
return res . redirect ( '/' ) ;
2015-04-14 15:36:02 -04:00
// json
2015-03-24 08:03:59 -07:00
} else if ( type === 'json' ) {
res . setHeader ( 'Content-Type' , 'application/json' ) ;
return res . send ( { message : message } ) ;
2015-04-14 15:36:02 -04:00
// plain text
2015-03-24 08:03:59 -07:00
} else {
res . setHeader ( 'Content-Type' , 'text/plain' ) ;
return res . send ( message ) ;
}
} ) ;
}
2014-11-19 15:30:36 -08:00
/ * *
* Start Express server .
* /
2015-04-22 14:53:58 -07:00
2015-01-16 18:58:27 -05:00
app . listen ( app . get ( 'port' ) , function ( ) {
2015-04-14 15:36:02 -04:00
console . log (
'FreeCodeCamp server listening on port %d in %s mode' ,
app . get ( 'port' ) ,
app . get ( 'env' )
) ;
2014-11-19 15:30:36 -08:00
} ) ;
module . exports = app ;