-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (63 loc) · 1.99 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
#!/usr/bin/env node
/**
* Register the static pages and API routes under NodeJS Express.
*/
const createError = require('http-errors')
const express = require('express')
const path = require('path')
const cookieParser = require('cookie-parser')
const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand')
const storedEnv = dotenv.config()
dotenvExpand.expand(storedEnv)
const logger = require('morgan')
const cors = require('cors')
const indexRouter = require('./routes/index.js')
var app = express()
//Middleware to use
/**
* Get the various CORS headers right
* "methods" : Allow
* "allowedMethods" : Access-Control-Allow-Methods (Allow ALL the methods)
* "allowedHeaders" : Access-Control-Allow-Headers (Allow custom headers)
* "exposedHeaders" : Access-Control-Expose-Headers (Expose the custom headers)
* "origin" : "*" : Access-Control-Allow-Origin (Allow ALL the origins)
* "maxAge" : "600" : Access-Control-Max-Age (how long to cache preflight requests, 10 mins)
*/
app.use(
cors({
"methods": "GET,OPTIONS,HEAD,PUT,PATCH,DELETE,POST",
"allowedHeaders": [
'Content-Type',
'Content-Length',
'Allow',
'Authorization',
'Location',
'ETag',
'Connection',
'Keep-Alive',
'Date',
'Cache-Control',
'Last-Modified',
'Link',
'X-HTTP-Method-Override'
],
"exposedHeaders": "*",
"origin": "*",
"maxAge": "600"
})
)
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(cookieParser())
//Publicly available scripts, CSS, and HTML pages.
app.use(express.static(path.join(__dirname, 'public')))
app.use('/', indexRouter)
//catch 404 because of an invalid site path
app.use(function(req, res, next) {
let msg = res.statusMessage ?? "This page does not exist"
res.status(404).send(msg)
res.end()
})
module.exports = app