app.ts 2.16 KB
Newer Older
Rosanny Sihombing's avatar
updates    
Rosanny Sihombing committed
1
2
3
4
5
import express from 'express'
import path from 'path'
import morgan from 'morgan'
import cookieParser from 'cookie-parser'
import bodyParser from 'body-parser'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
6
import helmet from 'helmet'
Rosanny Sihombing's avatar
updates    
Rosanny Sihombing committed
7
import compression from 'compression'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
8

Rosanny Sihombing's avatar
Rosanny Sihombing committed
9
const env = process.env.NODE_ENV ?? 'testing'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
10
const config = require('./config/config')[env]
Rosanny Sihombing's avatar
Rosanny Sihombing committed
11
const lang = 'DE'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
12

Rosanny Sihombing's avatar
Rosanny Sihombing committed
13
const app = express()
Rosanny Sihombing's avatar
Rosanny Sihombing committed
14
app.set('port', config.app.port)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
15
app.set('views', path.join(__dirname, '/views'))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
16
17
app.set('view engine', 'pug')

Rosanny Sihombing's avatar
Rosanny Sihombing committed
18
// app.use(helmet())
Rosanny Sihombing's avatar
Rosanny Sihombing committed
19
app.use(compression())
Rosanny Sihombing's avatar
Rosanny Sihombing committed
20
21
22
app.use(morgan('combined'))
app.use(cookieParser())
app.use(bodyParser.json())
Rosanny Sihombing's avatar
Rosanny Sihombing committed
23
app.use(bodyParser.urlencoded({ extended: false }))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
24
app.use(express.static(path.join(__dirname, 'public')))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

app.use(
  helmet.contentSecurityPolicy({
    useDefaults: true,
    directives: {
      'font-src': ["'self'", 'https://use.fontawesome.com'],
      'img-src': ["'self'", 'https://transfer.hft-stuttgart.de'],
      'script-src': ["'self'", 'https://code.jquery.com/jquery-3.3.1.min.js', 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js', 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js'],
      'style-src': ["'self'", 'https://use.fontawesome.com/releases/v5.8.2/css/all.css'],
      'frame-src': ["'self'"]
    },
    reportOnly: true
  })
)
app.use(compression())
app.use(morgan('combined'))
app.use(cookieParser())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, 'public')))

Rosanny Sihombing's avatar
Rosanny Sihombing committed
46
47
// caching disabled for every route
// NOTE: Works in Firefox and Opera. Does not work in Edge
Rosanny Sihombing's avatar
Rosanny Sihombing committed
48
49
50
app.use(function (req, res, next) {
  res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0')
  next()
Rosanny Sihombing's avatar
Rosanny Sihombing committed
51
})
Rosanny Sihombing's avatar
Rosanny Sihombing committed
52
53
54

require('./routes/project')(app)

Rosanny Sihombing's avatar
Rosanny Sihombing committed
55
// Handle 404
Rosanny Sihombing's avatar
Rosanny Sihombing committed
56
57
app.use(function (req: any, res: any) {
  res.status(404).render(lang + '/404')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
58
})
Rosanny Sihombing's avatar
Rosanny Sihombing committed
59

Rosanny Sihombing's avatar
Rosanny Sihombing committed
60
// Handle 500 - any server error
Rosanny Sihombing's avatar
Rosanny Sihombing committed
61
62
63
64
65
app.use(function (err: any, req: any, res: any, next: any) {
  console.error(err.stack)
  res.status(500).render(lang + '/500', {
    error: err
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
66
})
Rosanny Sihombing's avatar
Rosanny Sihombing committed
67

Rosanny Sihombing's avatar
Rosanny Sihombing committed
68
app.listen(app.get('port'), function () {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
69
70
  console.log('Project Page listening on port ' + String(app.get('port')))
})