2015-03-17 21:57:05 -07:00
if ( process . env . NODE _ENV !== 'development' ) {
require ( 'newrelic' ) ;
}
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-03-24 08:03:59 -07:00
accepts = require ( 'accepts' ) ,
2014-12-23 08:48:28 -08:00
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' ) ,
connectAssets = require ( 'connect-assets' ) ,
2015-04-08 22:42:40 -07:00
request = require ( 'request' ) ,
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' ) ,
challengesController = require ( './controllers/challenges' ) ,
resourcesController = require ( './controllers/resources' ) ,
userController = require ( './controllers/user' ) ,
contactController = require ( './controllers/contact' ) ,
2015-01-11 00:45:22 -05:00
bonfireController = require ( './controllers/bonfire' ) ,
2015-02-01 23:35:27 -08:00
coursewareController = require ( './controllers/courseware' ) ,
2013-11-14 02:29:55 -05:00
2015-03-03 19:23:56 +09:00
/ * *
* Stories
* /
2015-04-08 22:42:40 -07:00
storyController = require ( './controllers/story' ) ,
2015-03-03 19:23:56 +09: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' ) ;
2014-06-06 14:58:30 -04:00
app . use ( compress ( ) ) ;
2014-12-27 00:22:50 -08:00
var oneYear = 31557600000 ;
2015-01-16 18:58:27 -05:00
app . use ( express . static ( _ _dirname + '/public' , { maxAge : oneYear } ) ) ;
2014-02-21 17:29:06 -05:00
app . use ( connectAssets ( {
2014-12-23 08:48:28 -08:00
paths : [
2015-01-16 18:58:27 -05:00
path . join ( _ _dirname , 'public/css' ) ,
path . join ( _ _dirname , 'public/js' )
2014-12-23 08:48:28 -08:00
] ,
2014-10-17 19:23:53 -07:00
helperContext : app . locals
2014-02-03 08:34:12 -05:00
} ) ) ;
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 ( {
2014-10-17 19:23:53 -07:00
resave : true ,
saveUninitialized : true ,
secret : secrets . sessionSecret ,
store : new MongoStore ( {
url : secrets . db ,
2014-11-19 15:30:36 -08:00
'auto_reconnect' : true
2014-10-17 19:23:53 -07: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 ) {
res . header ( "Access-Control-Allow-Origin" , "*" ) ;
res . header ( "Access-Control-Allow-Headers" , "Origin, X-Requested-With, Content-Type, Accept" ) ;
next ( ) ;
} ) ;
2014-12-23 08:48:28 -08:00
2014-12-22 12:36:45 -08:00
var trusted = [
2014-12-23 13:30:20 -08:00
"'self'" ,
2014-12-22 12:36:45 -08:00
'*.freecodecamp.com' ,
2014-12-23 08:48:28 -08:00
'*.gstatic.com' ,
2015-01-05 17:29:37 -08:00
'*.google-analytics.com' ,
'*.googleapis.com' ,
'*.google.com' ,
'*.gstatic.com' ,
'*.doubleclick.net' ,
'*.twitter.com' ,
2015-01-14 13:26:36 -08:00
'*.twitch.tv' ,
2014-12-22 12:36:45 -08:00
'*.twimg.com' ,
"'unsafe-eval'" ,
2014-12-22 15:07:32 -08:00
"'unsafe-inline'" ,
2015-01-05 17:29:37 -08:00
'*.rafflecopter.com' ,
2015-01-06 10:28:57 -05:00
'*.bootstrapcdn.com' ,
2015-01-05 17:29:37 -08:00
'*.cloudflare.com' ,
2015-03-17 16:18:41 -07:00
'https://*.cloudflare.com' ,
2015-01-06 10:28:57 -05:00
'localhost:3001' ,
2015-01-09 07:53:29 -08:00
'ws://localhost:3001/' ,
'http://localhost:3001' ,
'localhost:3000' ,
'ws://localhost:3000/' ,
2015-01-09 08:47:49 -08:00
'http://localhost:3000' ,
2015-01-09 15:47:52 -08:00
'*.ionicframework.com' ,
2015-01-11 00:07:41 -08:00
'https://syndication.twitter.com' ,
2015-01-22 16:52:43 -08:00
'*.youtube.com' ,
2015-02-21 14:22:46 -08:00
'*.jsdelivr.net' ,
2015-03-17 16:18:41 -07:00
'https://*.jsdelivr.net' ,
2015-02-21 14:22:46 -08:00
'*.togetherjs.com' ,
'https://*.togetherjs.com' ,
'wss://hub.togetherjs.com' ,
'*.ytimg.com' ,
2015-02-23 08:01:55 +09:00
'wss://fcctogether.herokuapp.com' ,
'*.bitly.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-01-11 00:07:41 -08:00
scriptSrc : [ '*.optimizely.com' , '*.aspnetcdn.com' ] . concat ( trusted ) ,
2014-12-23 13:50:14 -08:00
'connect-src' : [
2015-01-16 18:58:27 -05:00
'ws://*.rafflecopter.com' ,
'wss://*.rafflecopter.com' ,
'https://*.rafflecopter.com' ,
'ws://www.freecodecamp.com' ,
'http://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-01-16 18:58:27 -05:00
'*.evernote.com' ,
'*.amazonaws.com' ,
'data:' ,
'*.licdn.com' ,
'*.gravatar.com' ,
'*.akamaihd.net' ,
'graph.facebook.com' ,
'*.githubusercontent.com' ,
'*.googleusercontent.com' ,
'*' /* allow all input since we have user submitted images for public profile*/
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' ,
'*.rafflecopter.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 ) {
2014-10-17 19:23:53 -07:00
// Make user object available in templates.
2015-03-19 15:28:20 -07:00
res . locals . user = req . user ;
2014-10-17 19:23:53 -07:00
next ( ) ;
2014-01-11 22:53:31 -05:00
} ) ;
2014-11-19 15:30:36 -08:00
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 ] ;
if ( /auth|login|logout|signup|fonts|favicon/i . test ( path ) ) {
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-12-23 08:48:28 -08:00
app . use (
2015-01-16 18:58:27 -05:00
express . static ( path . join ( _ _dirname , 'public' ) , { maxAge : 31557600000 } )
2014-12-23 08:48:28 -08:00
) ;
2014-01-08 01:37:40 -05:00
2015-03-07 02:00:21 -08:00
app . use ( express . static ( _ _dirname + '/public' , { maxAge : 86400000 } ) ) ;
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 ) ;
2014-11-29 23:01:49 -08:00
app . get ( '/privacy' , resourcesController . privacy ) ;
2014-11-29 15:16:47 -08:00
app . get ( '/jquery-exercises' , resourcesController . jqueryExercises ) ;
2015-02-19 16:33:08 -08:00
app . get ( '/chat' , resourcesController . chat ) ;
2014-12-14 22:24:54 -08:00
app . get ( '/live-pair-programming' , resourcesController . livePairProgramming ) ;
2015-02-17 14:10:15 -08:00
app . get ( '/install-screenhero' , resourcesController . installScreenHero ) ;
2014-12-14 22:24:54 -08:00
app . get ( '/javascript-in-your-inbox' , resourcesController . javaScriptInYourInbox ) ;
2015-03-08 01:36:09 -08:00
app . get ( '/guide-to-our-nonprofit-projects' , resourcesController . guideToOurNonprofitProjects ) ;
2014-12-11 21:24:44 -08:00
app . get ( '/chromebook' , resourcesController . chromebook ) ;
2015-03-29 17:28:25 -07:00
app . get ( '/styleguide' , resourcesController . styleguide ) ;
2014-12-26 00:05:00 -08:00
app . get ( '/deploy-a-website' , resourcesController . deployAWebsite ) ;
app . get ( '/gmail-shortcuts' , resourcesController . gmailShortcuts ) ;
app . get ( '/control-shortcuts' , resourcesController . controlShortcuts ) ;
app . get ( '/control-shortcuts' , resourcesController . deployAWebsite ) ;
2015-03-19 22:34:36 -07:00
app . get ( '/nodeschool-challenges' , resourcesController . nodeSchoolChallenges ) ;
2015-01-26 11:38:19 -08:00
app . get ( '/stats' , function ( req , res ) {
res . redirect ( 301 , '/learn-to-code' ) ;
} ) ;
2015-03-09 08:54:45 -07:00
app . get ( '/news' , function ( req , res ) {
res . redirect ( 301 , '/stories/hot' ) ;
} ) ;
2015-01-17 11:27:27 -08:00
app . get ( '/learn-to-code' , resourcesController . about ) ;
2015-01-26 11:38:19 -08:00
app . get ( '/about' , function ( req , res ) {
res . redirect ( 301 , '/learn-to-code' ) ;
} ) ;
app . get ( '/signin' , userController . getSignin ) ;
app . get ( '/login' , function ( req , res ) {
res . redirect ( 301 , '/signin' ) ;
} ) ;
app . post ( '/signin' , userController . postSignin ) ;
app . get ( '/signout' , userController . signout ) ;
app . get ( '/logout' , function ( req , res ) {
res . redirect ( 301 , '/signout' ) ;
} ) ;
2014-06-06 15:23:28 -04:00
app . get ( '/forgot' , userController . getForgot ) ;
app . post ( '/forgot' , userController . postForgot ) ;
app . get ( '/reset/:token' , userController . getReset ) ;
app . post ( '/reset/:token' , userController . postReset ) ;
2014-12-07 16:25:43 -08:00
app . get ( '/email-signup' , userController . getEmailSignup ) ;
app . get ( '/email-signin' , userController . getEmailSignin ) ;
app . post ( '/email-signup' , userController . postEmailSignup ) ;
2015-01-26 11:38:19 -08:00
app . post ( '/email-signin' , userController . postSignin ) ;
2015-01-14 13:28:20 -08:00
app . get ( '/nonprofits' , contactController . getNonprofitsForm ) ;
app . post ( '/nonprofits' , contactController . postNonprofitsForm ) ;
2015-01-23 15:13:36 -08:00
2015-01-14 13:28:20 -08:00
app . get (
'/done-with-first-100-hours' ,
2015-01-23 15:13:36 -08:00
passportConf . isAuthenticated ,
2015-01-14 13:28:20 -08:00
contactController . getDoneWithFirst100Hours
) ;
app . post (
'/done-with-first-100-hours' ,
2015-01-23 15:13:36 -08:00
passportConf . isAuthenticated ,
2015-01-14 13:28:20 -08:00
contactController . postDoneWithFirst100Hours
) ;
2015-01-23 15:13:36 -08:00
app . get (
'/nonprofit-project-instructions' ,
passportConf . isAuthenticated ,
resourcesController . nonprofitProjectInstructions
) ;
2014-11-19 15:50:57 -08:00
app . post (
2015-01-16 18:58:27 -05: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-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' ) ;
}
} )
} 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-03-03 19:23:56 +09:00
/ * *
* Main routes .
* /
2015-03-06 08:11:18 +09:00
app . get (
'/stories/hotStories' ,
storyController . hotJSON
) ;
app . get (
'/stories/recentStories' ,
storyController . recentJSON
) ;
2015-03-03 19:23:56 +09:00
app . get (
'/stories/' ,
2015-03-06 08:11:18 +09:00
function ( req , res ) {
res . redirect ( 302 , '/stories/hot' ) ;
}
2015-03-03 19:23:56 +09:00
) ;
2015-03-06 08:11:18 +09:00
2015-03-03 19:23:56 +09:00
app . get (
2015-03-06 08:11:18 +09: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-03-07 01:57:09 +09:00
'/stories/comment/' ,
2015-03-06 06:08:40 +09:00
storyController . commentSubmit
) ;
2015-03-07 01:57:09 +09:00
app . post (
'/stories/comment/:id/comment' ,
storyController . commentOnCommentSubmit
) ;
2015-03-04 07:15:00 +09:00
app . get (
2015-03-06 08:11:18 +09:00
'/stories/submit' ,
storyController . submitNew
2015-03-04 07:15:00 +09:00
) ;
2015-03-07 17:42:22 +09:00
app . get (
2015-03-09 18:41:07 +09:00
'/stories/submit/new-story' ,
2015-03-07 17:42:22 +09:00
storyController . preSubmit
2015-03-05 19:21:26 +09:00
) ;
2015-03-07 17:42:22 +09:00
app . post (
'/stories/preliminary' ,
storyController . newStory
) ;
2015-03-06 08:11:18 +09:00
2015-03-07 18:26:49 +09:00
app . post (
'/stories/' ,
storyController . storySubmission
) ;
2015-03-06 08:11:18 +09:00
app . get (
'/stories/hot' ,
storyController . hot
) ;
app . get (
'/stories/recent' ,
storyController . recent
) ;
app . get (
'/stories/search' ,
storyController . search
) ;
2015-03-06 09:20:30 +09:00
app . post (
'/stories/search' ,
storyController . getStories
) ;
2015-03-03 19:50:16 +09:00
app . get (
'/stories/:storyName' ,
storyController . returnIndividualStory
) ;
2015-03-06 08:11:18 +09:00
2015-03-03 22:03:33 +09:00
app . post (
2015-03-04 07:15:00 +09:00
'/stories/upvote/' ,
2015-03-03 22:03:33 +09:00
storyController . upvote
) ;
2015-03-03 19:23:56 +09:00
2015-02-15 23:59:03 -08:00
/ * *
* Challenge related routes
* /
app . get (
'/challenges/' ,
challengesController . returnNextChallenge
) ;
2014-11-19 15:50:57 -08:00
app . get (
'/challenges/:challengeNumber' ,
2014-12-23 12:34:09 -08:00
challengesController . returnChallenge
2014-12-23 08:48:28 -08:00
) ;
2015-02-15 23:59:03 -08:00
2014-11-19 15:30:36 -08:00
app . all ( '/account' , passportConf . isAuthenticated ) ;
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-27 23:30:06 -07:00
app . get ( '/api/blogger' , resourcesController . bloggerCalls ) ;
app . get ( '/api/trello' , resourcesController . trelloCalls ) ;
2015-02-16 23:35:02 -08:00
2015-01-27 20:12:51 -05:00
/ * *
* Bonfire related routes
* /
2015-01-24 14:42:34 -08:00
app . get ( '/playground' , bonfireController . index ) ;
2015-01-27 20:12:51 -05:00
app . get ( '/bonfires' , bonfireController . returnNextBonfire ) ;
app . get ( '/bonfire-json-generator' , bonfireController . returnGenerator ) ;
app . post ( '/bonfire-json-generator' , bonfireController . generateChallenge ) ;
app . get ( '/bonfire-challenge-generator' , bonfireController . publicGenerator ) ;
app . post ( '/bonfire-challenge-generator' , bonfireController . testBonfire )
2015-01-22 13:20:46 -05:00
app . get (
2015-01-26 18:28:14 -05:00
'/bonfires/:bonfireName' ,
2015-01-24 14:29:50 -08:00
bonfireController . returnIndividualBonfire
2015-01-22 13:20:46 -05:00
) ;
2015-01-26 12:30:04 -05:00
app . get ( '/bonfire' , function ( req , res ) {
res . redirect ( 301 , '/playground' ) ;
} ) ;
2014-02-01 03:30:14 -05:00
2015-02-01 22:39:59 -08:00
app . post ( '/completed-bonfire/' , bonfireController . completedBonfire ) ;
2015-01-24 00:44:08 -05:00
2015-02-01 23:35:27 -08:00
/ * *
* Courseware related routes
* /
2015-02-06 12:36:55 -05:00
app . get ( '/coursewares/' , coursewareController . returnNextCourseware ) ;
2015-02-01 23:35:27 -08:00
app . get (
'/coursewares/:coursewareName' ,
coursewareController . returnIndividualCourseware
) ;
app . post ( '/completed-courseware/' , coursewareController . completedCourseware ) ;
2015-01-27 20:12:51 -05:00
// Unique Check API route
app . get ( '/api/checkUniqueUsername/:username' , userController . checkUniqueUsername ) ;
app . get ( '/api/checkExistingUsername/:username' , userController . checkExistingUsername ) ;
app . get ( '/api/checkUniqueEmail/:email' , userController . checkUniqueEmail ) ;
app . get ( '/account' , userController . getAccount ) ;
app . post ( '/account/profile' , userController . postUpdateProfile ) ;
app . post ( '/account/password' , userController . postUpdatePassword ) ;
app . post ( '/account/delete' , userController . postDeleteAccount ) ;
app . get ( '/account/unlink/:provider' , userController . getOauthUnlink ) ;
2015-03-06 00:20:39 -08:00
app . get ( '/sitemap.xml' , resourcesController . sitemap ) ;
2015-01-27 20:12:51 -05:00
/ * *
* API examples routes .
* accepts a post request . the challenge id req . body . challengeNumber
* and updates user . challengesHash & user . challengesCompleted
*
* /
2015-03-23 17:17:39 -07:00
app . post ( '/completed-challenge' , function ( req , res , done ) {
2015-01-27 20:12:51 -05:00
req . user . challengesHash [ parseInt ( req . body . challengeNumber ) ] =
Math . round ( + new Date ( ) / 1000 ) ;
var timestamp = req . user . challengesHash ;
var points = 0 ;
for ( var key in timestamp ) {
2015-02-08 12:39:00 -05:00
if ( timestamp [ key ] > 0 && req . body . challengeNumber < 54 ) {
2015-01-27 20:12:51 -05:00
points += 1 ;
}
}
req . user . points = points ;
2015-03-23 17:17:39 -07:00
req . user . save ( function ( err ) {
if ( err ) { return done ( err ) ; }
res . status ( 200 ) . send ( { msg : 'progress saved' } ) ;
} ) ;
2015-01-27 20:12:51 -05: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-01-16 18:58:27 -05: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' ) ) ;
2014-11-19 15:30:36 -08:00
app . get (
2015-01-16 18:58:27 -05: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-01-16 18:58:27 -05: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-01-16 18:58:27 -05: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-01-16 18:58:27 -05:00
'/auth/facebook' ,
passport . authenticate ( 'facebook' , { scope : [ 'email' , 'user_location' ] } )
2014-12-23 08:48:28 -08:00
) ;
app . get (
2015-01-16 18:58:27 -05: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' ) ) ;
2014-12-23 08:48:28 -08:00
app . get (
2015-01-16 18:58:27 -05: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-01-16 18:58:27 -05:00
'/auth/google' ,
passport . authenticate ( 'google' , { scope : 'profile email' } )
2014-12-23 08:48:28 -08:00
) ;
app . get (
2015-01-16 18:58:27 -05: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
app . get ( '/induce-vomiting' , function ( req , res , next ) {
next ( new Error ( 'vomiting induced' ) ) ;
} ) ;
// put this route last
2015-01-17 18:52:58 -08:00
app . get (
'/:username' ,
userController . returnUser
) ;
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 ( '/' ) ;
// json
} else if ( type === 'json' ) {
res . setHeader ( 'Content-Type' , 'application/json' ) ;
return res . send ( { message : message } ) ;
// plain text
} else {
res . setHeader ( 'Content-Type' , 'text/plain' ) ;
return res . send ( message ) ;
}
} ) ;
}
2014-11-19 15:30:36 -08:00
/ * *
* Start Express server .
* /
2015-01-16 18:58:27 -05:00
app . listen ( app . get ( 'port' ) , function ( ) {
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 ;