-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.mjs
134 lines (117 loc) · 2.91 KB
/
server.mjs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as fs from 'node:fs'
import {createServer} from 'http'
import {createRequestHandler} from '@remix-run/express'
import {installGlobals} from '@remix-run/node'
import chalk from 'chalk'
import express from 'express'
import morgan from 'morgan'
const isProd = process.env.NODE_ENV === 'production'
const start = Date.now()
let viteVersion
let remixVersion
if (process.env.NODE_ENV !== 'production') {
// get the vite version from the vite package.json
viteVersion = JSON.parse(
fs.readFileSync('node_modules/vite/package.json'),
).version
remixVersion = JSON.parse(
fs.readFileSync('node_modules/@remix-run/dev/package.json'),
).version
}
installGlobals()
let vite = isProd
? undefined
: await import('vite').then(({createServer}) =>
createServer({
server: {
middlewareMode: true,
},
}),
)
const app = express()
const httpServer = createServer(app)
if (vite) {
app.use(vite.middlewares)
} else {
app.use(morgan('tiny'))
app.use(
'/assets',
express.static('build/client/assets', {immutable: true, maxAge: '1y'}),
)
}
app.use(express.static('build/client', {maxAge: '1h'}))
app.get('/healthcheck', (_, res) => {
console.log('Health check succeeded', Date.now())
res.statusCode = 200
res.sendStatus(200)
})
const port = parseInt(process.env.PORT || '', 10) || 3000
httpServer.listen(port, async () => {
if (!isProd) {
console.log(
chalk.greenBright.bold('VITE'),
chalk.green(`v${viteVersion}`),
chalk.blueBright.bold('Remix'),
chalk.blue(`v${remixVersion}`),
chalk.dim('ready in'),
)
}
console.log(
' ',
chalk.greenBright.bold('➜'),
chalk.bold('Local:'),
chalk.cyan(`http://localhost:${port}`),
)
if (process.env.SKIP_REMIX !== 'true') {
console.log(
' ',
chalk.greenBright.bold('➜'),
chalk.bold('Checkmate:'),
chalk.green('Enabled'),
)
try {
if (vite) {
app.all(
'*',
createRequestHandler({
build: () => vite.ssrLoadModule('virtual:remix/server-build'),
}),
)
console.log(
' ',
chalk.dim('Server ready in'),
chalk.dim.bold(`${Date.now() - start}ms`),
)
console.log()
} else {
import('./build/server/index.js')
.then((build) => {
app.all(
'*',
createRequestHandler({
build,
}),
)
console.log(
' ',
chalk.dim('Server ready in'),
chalk.dim.bold(`${Date.now() - start}ms`),
)
console.log()
})
.catch((e) => {
throw e
})
}
} catch (e) {
process.exit(1)
}
} else {
console.log(
' ',
chalk.greenBright.bold('➜'),
chalk.bold('Checkmate:'),
chalk.red('Disabled'),
)
}
})