forked from sebthedev/PrincetonCourses
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
140 lines (116 loc) · 4.28 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This script is the main app script that powers Princeton Courses. It runs immediately when the app launches
// Greet the world!
console.log('Launching Princeton Courses.')
// Load Node.js components
const path = require('path')
// Load external dependencies
let mongoose = require('mongoose')
let express = require('express')
let session = require('cookie-session')
// Make Mongoose use native promises
mongoose.Promise = global.Promise
// Initialise Express, which makes the server work
let app = express()
// Initialise bodyParser, which parses the data out of web requests
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
// Load internal modules
let config = require('./controllers/config')
let auth = require('./controllers/authentication.js')
let api = require('./controllers/api.js')
// Connect to the database
require('./controllers/database.js')
// Configure the app to save a cookie
app.use(session({
secret: config.sessionSecret,
maxAge: 24 * 60 * 60 * 1000 * 365 // 365 days
}))
// Attempt to load the currently logged-in user
app.use('*', auth.loadUser)
// Pre-compose the variables that will be sent to the page to render
app.use('*', function (req, res, next) {
res.locals.renderLocals = {}
if (res.locals.user) {
res.locals.renderLocals.netid = res.locals.user._id
}
if (process.env.NODE_ENV) {
res.locals.renderLocals.environment = process.env.NODE_ENV
}
if (process.env.HEROKU_RELEASE_VERSION) {
res.locals.renderLocals.release = process.env.HEROKU_RELEASE_VERSION
} else {
res.locals.renderLocals.release = 'v0'
}
next()
})
// Attach routers (these are modules that contain a distinct set of routes)
app.use('/auth', auth.router)
app.use('/api', api.router)
// Route a request for the homepage
app.get('/', function (req, res) {
// Check whether the user sending this request is authenticated
if (!auth.userIsAuthenticated(req)) {
// The user in unauthenticated. Display a splash page.
res.render('pages/splash', res.locals.renderLocals)
} else {
// The user has authenticated. Display the app
res.render('pages/app', res.locals.renderLocals)
}
})
const crawlerRegex = new RegExp('(facebookexternalhit|twitterbot|facebot)')
const Course = require('./models/course.js')
// Route a request for a page inside the app
app.get('/course/:id', express.urlencoded({extended: true}), function (req, res) {
// Check whether the user sending this request is authenticated
if (!auth.userIsAuthenticated(req)) {
// Dislay a simple page with crawler markup for Facebook, Twitter, and iMessage crawlers
if (crawlerRegex.test(req.get('User-Agent'))) {
return Course.findById(req.params.id).then(course => {
if (!course) {
return res.redirect('/auth/login?redirect=' + req.originalUrl)
}
return res.render('pages/crawlerCourse', {course})
})
}
// Redirect to the login URL
return res.redirect('/auth/login?redirect=' + req.originalUrl)
} else {
// The user has authenticated. Display the app
return res.render('pages/app', res.locals.renderLocals)
}
})
// Route a request for the about page
app.get('/about', function (req, res) {
res.render('pages/about', res.locals.renderLocals)
})
// Route a request for the main app page
app.get('/app', function (req, res) {
res.render('pages/app', res.locals.renderLocals)
})
// Route a request for the main app page
app.get('/privacy', function (req, res) {
res.render('pages/privacy', res.locals.renderLocals)
})
// Map any files in the /public folder to the root of our domain
// For example, if there is a file at /public/cat.jpg of this app,
// it can be accessed on the web at [APP DOMAIN NAME]/cat.jpg
app.use(express.static(path.join(__dirname, '/public')))
app.get('*', function (req, res) {
res.sendStatus(404)
})
// Configure the EJS templating system (http://www.embeddedjs.com)
app.set('view engine', 'ejs')
const cluster = require('cluster')
if (cluster.isMaster) {
const os = require('os')
const workers = process.env.WEB_CONCURRENCY || os.cpus().length || 1
console.log(`Forking for ${workers} workers`)
for (let i = 0; i < workers; i++) {
cluster.fork()
}
} else {
// Start listening for requests
app.listen(config.port, function () {
console.log('Listening for reqs on port %d.', config.port)
})
}