-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
48 lines (41 loc) · 1.32 KB
/
server.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
/* eslint-disable no-console */
const dotenv = require('dotenv')
const path = require('path')
if (process.env.NODE_ENV !== 'production') {
dotenv.config({ path: path.join(__dirname, '/.env') })
}
require('newrelic')
const express = require('express')
const morgan = require('morgan')
const favicon = require('serve-favicon')
const router = require('./routes')
const requireSSL = (req, res, next) => {
if (
!req.secure
&& req.get('x-forwarded-proto') !== 'https'
&& process.env.NODE_ENV === 'production'
) {
return res.redirect(`https://${req.get('host')}${req.url}`)
}
return next()
}
const app = express()
app.set('view engine', 'pug')
app.use(morgan('dev'))
app.use(requireSSL)
app.use('/static', express.static(path.join(__dirname, 'public')))
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
// Log who is accessing Icotar
app.use((req, res, next) => {
const { url, ip, hostname } = req
const reqPath = req.path
const referrer = req.get('Referrer')
const userAgent = req.get('User-Agent')
console.log(`url=${url} path=${reqPath} ip=${ip} hostname=${hostname} referrer=${referrer} userAgent=${userAgent}`)
next()
})
app.use('/', router)
const port = process.env.PORT || 5000
const env = app.get('env')
app.listen(port, () => console
.log(`App is listening on port ${port} in ${env} mode!`))