-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
107 lines (89 loc) · 2.78 KB
/
Copy pathserver.js
File metadata and controls
107 lines (89 loc) · 2.78 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
107
import express from "express"
import portfinder from "portfinder"
import { loadConfig, createService } from "./index.js"
import htmlRoute from "./routes/html.js"
import validateRoute from "./routes/validate.js"
import formatsRoute from "./routes/formats.js"
import languagesRoute from "./routes/languages.js"
import schemaRoute from "./routes/schema.js"
import bytes from "bytes"
const config = loadConfig()
const { logger, limit } = config
logger.info(`Running in ${config.env} mode.`)
// Initialize express with settings
const app = express()
app.set("json spaces", 2)
if (config.proxies && config.proxies.length) {
app.set("trust proxy", config.proxies)
}
// Configure view engine to render EJS templates.
app.set("views", "./views")
app.set("view engine", "ejs")
// Middleware to parse the raw body
import typeis from "type-is"
app.use(
express.raw({
limit,
verify: (req, res, buf, encoding) => {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || "utf8")
}
},
type: req => !typeis(req, ["multipart"]),
}),
)
// Middleware to handle multipart request, including file upload
import multer from "multer"
const fileSize = bytes.parse(limit)
const upload = multer({ limits: { fileSize } })
app.use(upload.single("file"))
// Add default headers
app.use((req, res, next) => {
if (req.headers.origin) {
res.setHeader("Access-Control-Allow-Origin", req.headers.origin)
} else {
res.setHeader("Access-Control-Allow-Origin", "*")
}
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
res.setHeader("Access-Control-Allow-Methods", "GET,POST")
next()
})
// Enable routes
app.use("/", htmlRoute)
app.use("/", validateRoute)
app.get("/formats", formatsRoute)
app.get("/languages", languagesRoute)
app.get("/schema", schemaRoute)
// Error handling
app.use((error, req, res, next) => { // eslint-disable-line no-unused-vars
const response = {
error: error.constructor.name,
message: error.message,
code: error.code || 500,
}
/* c8 ignore next 3 */
if (config.env === "development" && error.stack) {
response.stack = error.stack.split("\n")
}
res.status(response.code).send(response)
})
// Start service
const start = async () => {
// Find available port on test and debug
let port = config.port
if (config.env == "test" || config.env === "debug") {
portfinder.basePort = config.port
port = await portfinder.getPortPromise()
}
config.baseUrl = config.baseUrl || `http://localhost:${port}/`
app.set("validationConfig", config)
// Initialize formats registry
const service = await createService(config)
app.set("validationService", service)
// Let's go!
app.listen(port, () => {
logger.info(`Now listening on port ${port}`)
})
}
start()
export default app