-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
106 lines (95 loc) · 2.69 KB
/
server.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
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
/* eslint-disable antfu/no-top-level-await */
/* eslint-disable no-console */
import process from 'node:process'
import closeWithGrace from 'close-with-grace'
import compression from 'compression'
import express from 'express'
import rateLimit from 'express-rate-limit'
// Short-circuit the type-checking of the built output.
const BUILD_PATH = './build/server/index.js'
const DEVELOPMENT = process.env.NODE_ENV === 'development'
const PORT = Number.parseInt(process.env.PORT || '3000')
const app = express()
app.set('trust proxy', 1)
app.disable('x-powered-by')
// Rate limiting
const maxMultiple = 1
const rateLimitDefault = {
windowMs: 60 * 1000,
max: 1000 * maxMultiple,
standardHeaders: true,
legacyHeaders: false,
}
const strongestRateLimit = rateLimit({
...rateLimitDefault,
windowMs: 60 * 1000,
max: 10 * maxMultiple,
})
const strongRateLimit = rateLimit({
...rateLimitDefault,
windowMs: 60 * 1000,
max: 100 * maxMultiple,
})
const generalRateLimit = rateLimit(rateLimitDefault)
app.use((req, res, next) => {
const strongPaths = [
'/login',
'/signup',
'/verify',
'/admin',
'/reset-password',
]
if (req.method !== 'GET' && req.method !== 'HEAD') {
if (strongPaths.some(p => req.path.includes(p))) {
return strongestRateLimit(req, res, next)
}
return strongRateLimit(req, res, next)
}
// the verify route is a special case because it's a GET route that
// can have a token in the query string
if (req.path.includes('/verify')) {
return strongestRateLimit(req, res, next)
}
return generalRateLimit(req, res, next)
})
app.use(compression())
if (DEVELOPMENT) {
console.log('Starting development server')
const viteDevServer = await import('vite').then(vite =>
vite.createServer({
server: { middlewareMode: true },
}),
)
app.use(viteDevServer.middlewares)
app.use(async (req, res, next) => {
try {
const source = await viteDevServer.ssrLoadModule('./server/app.ts')
return await source.app(req, res, next)
}
catch (error) {
if (typeof error === 'object' && error instanceof Error) {
viteDevServer.ssrFixStacktrace(error)
}
next(error)
}
})
}
else {
console.log('Starting production server')
app.use(
'/assets',
express.static('build/client/assets', { immutable: true, maxAge: '1y' }),
)
app.use(express.static('build/client', { maxAge: '1h' }))
app.use(await import(BUILD_PATH).then(mod => mod.app))
}
const server = app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`)
})
closeWithGrace({
delay: 3000,
}, async () => {
await new Promise((resolve, reject) => {
server.close(e => (e ? reject(e) : resolve('ok')))
})
})