-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
106 lines (93 loc) · 3.15 KB
/
app.js
File metadata and controls
106 lines (93 loc) · 3.15 KB
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
#!/usr/bin/env node
import express from 'express'
import path from 'path'
import cookieParser from 'cookie-parser'
import dotenv from 'dotenv'
dotenv.config()
import logger from 'morgan'
import cors from 'cors'
import indexRouter from './routes/index.js'
import apiRouter from './routes/api-routes.js'
import clientRouter from './routes/client.js'
import _gog_fragmentsRouter from './routes/_gog_fragments_from_manuscript.js';
import _gog_glossesRouter from './routes/_gog_glosses_from_manuscript.js';
import rest from './rest.js'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const 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',
'Origin',
'Referrer',
'User-Agent'
],
"exposedHeaders" : "*",
"origin" : "*",
"maxAge" : "600"
})
)
app.use(logger('dev'))
app.use(express.json({ type: ["application/json", "application/ld+json"] }))
app.use(express.text())
app.use(cookieParser())
//Publicly available scripts, CSS, and HTML pages.
app.use(express.static(path.join(__dirname, 'public')))
/**
* For any request that comes through to the app, check whether or not we are in maintenance mode.
* If we are, then show the sad puppy. Otherwise, continue on.
* This is without middleware
*/
app.all('*_', (req, res, next) => {
if(process.env.DOWN === "true"){
res.status(503).json({"message":"RERUM v1 is down for updates or maintenance at this time. We apologize for the inconvenience. Try again later."})
}
else{
next() //pass on to the next app.use
}
})
app.use('/', indexRouter)
app.use('/v1', apiRouter)
app.use('/client', clientRouter)
app.use('/gog/fragmentsInManuscript', _gog_fragmentsRouter)
app.use('/gog/glossesInManuscript', _gog_glossesRouter)
/**
* Handle API errors and warnings RESTfully. All routes that don't end in res.send() will end up here.
* Important to note that res.json() will fail to here
* Important to note api-routes.js handles all the 405s without failing to here - they res.send()
* Important to note that failures in the controller from the mongo client will fail to here
*
* */
app.use(rest.messenger)
//catch 404 because of an invalid site path
app.use((req, res, next) => {
let msg = res.statusMessage ?? "This page does not exist"
res.status(404).send(msg)
res.end()
})
export default app