-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
103 lines (91 loc) · 2.65 KB
/
index.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
'use strict'
const path = require('path')
const express = require('express')
const cors = require('cors')
const nunjucks = require('nunjucks')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const session = require('express-session')
const flash = require('connect-flash')
const config = require('./config')
const routes = require('./routes')
module.exports.makeApp = function () {
const app = express()
app.set('config', config)
app.set('port', config.get('app:port'))
app.set('views', path.join(__dirname, '/views'))
// Middlewares
app.use('/static', express.static(path.join(__dirname, '/public')))
app.use(
bodyParser.urlencoded({
extended: true
})
)
app.use(cors())
app.use(cookieParser())
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
app.use(flash())
// Check if looged in and set locals for nunjucks
app.use((req, res, next) => {
// Check if credentials are passed in query params:
if (req.query.jwt) {
res.cookie('jwt', req.query.jwt)
res.cookie('email', req.query.email)
res.cookie('id', req.query.id)
res.cookie('username', req.query.username)
res.locals.login = true
} else if (req.cookies.jwt) { // Else check if jwt in cookies
res.locals.login = true
} else { // Otherwise user is not logged in
res.locals.login = false
}
res.locals.message = req.flash('message')
next()
})
// Redirect x/y/ to x/y
app.use((req, res, next) => {
if(req.url.substr(-1) === '/' && req.url.length > 1) {
res.redirect(301, req.url.slice(0, req.url.length-1))
}
else {
next()
}
})
// Controllers
app.use([
routes()
])
app.use((err, req, res, next) => {
if (err.status === 404 || err.name === 'Forbidden') {
res.status(404).render('404.html', {
message: 'Sorry, this page was not found',
comment: 'You might need to Login to access more datasets'
})
return
} else {
console.error(err)
res.status(500).send( 'Something failed. Please, try again later.')
}
})
// eslint-disable-next-line no-unused-vars
const env = nunjucks.configure(app.get('views'), {
autoescape: true,
express: app
})
return app
}
module.exports.start = function () {
// eslint-disable-next-line no-unused-vars
return new Promise((resolve, reject) => {
const app = module.exports.makeApp()
let server = app.listen(app.get('port'), () => {
console.log('Listening on :' + app.get('port'))
resolve(server)
})
app.shutdown = function () {
server.close()
server = null
}
})
}
module.exports.start()