-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
97 lines (83 loc) · 2.67 KB
/
app.js
File metadata and controls
97 lines (83 loc) · 2.67 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
#!/usr/bin/env node
/** Server initializer for the app. Registers all the route paths. */
import express from 'express'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
import cookieParser from 'cookie-parser'
import cors from 'cors'
import logger from 'morgan'
import indexRouter from './index.js'
import projectRouter from './project/index.js'
import pageRouter from './page/index.js'
import lineRouter from './line/index.js'
import userProfileRouter from './userProfile/index.js'
import privateProfileRouter from './userProfile/privateProfile.js'
import proxyRouter from './utilities/proxy.js'
import feedbackRouter from './feedback/feedbackRoutes.js'
import routeErrorHandler from './utilities/routeErrorHandler.js'
import { respondWithError } from './utilities/shared.js'
let app = express()
// CORS configuration - Open to all origins
const corsOptions = {
origin: true, // Allow all origins
credentials: true,
optionsSuccessStatus: 200,
methods : 'GET,OPTIONS,HEAD,PUT,PATCH,DELETE,POST',
allowedHeaders : [
'Content-Type',
'Content-Length',
'Allow',
'Authorization',
'Location',
'Connection',
'Keep-Alive',
'Date',
'Cache-Control',
'Last-Modified',
'Link',
'Origin',
'Referrer',
'User-Agent'
],
exposedHeaders: '*'
}
//Middleware to use
app.use(cors(corsOptions))
app.use(logger('dev'))
app.use(express.json())
app.use(express.text())
app.use(express.urlencoded({ extended: true }))
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 respond with a 503 and a message. Otherwise, continue on.
*/
app.all('*_', (req, res, next) => {
if (process.env.DOWN === 'true') {
return respondWithError(
res,
503,
'TPEN3 services are down for updates or maintenance at this time. We apologize for the inconvenience. Try again later.'
)
}
next()
})
app.use('/', indexRouter)
app.use('/project/:projectId/page/:pageId/line', lineRouter)
app.use('/project/:projectId/page', pageRouter)
app.use('/project', projectRouter)
app.use('/user', userProfileRouter)
app.use('/my', privateProfileRouter)
app.use('/proxy', proxyRouter)
app.use('/beta', feedbackRouter)
// Centralized error handling middleware
app.use(routeErrorHandler)
//catch 404 because of an invalid site path
app.use('*_', (req, res) => {
return respondWithError(res, 404, res.statusMessage ?? 'This page does not exist')
})
export { app as default }