-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathserver.mjs
85 lines (75 loc) · 2.1 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
import * as fs from 'node:fs'
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 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 =
process.env.NODE_ENV === 'production'
? undefined
: await import('vite').then(({ createServer }) =>
createServer({
server: {
middlewareMode: true,
},
}),
)
const app = express()
// handle asset requests
if (vite) {
app.use(vite.middlewares)
} else {
// add morgan here for production only
// dev uses morgan plugin, otherwise it spams the console with HMR requests
app.use(morgan('tiny'))
app.use(
'/assets',
express.static('build/client/assets', { immutable: true, maxAge: '1y' }),
)
}
app.use(express.static('build/client', { maxAge: '1h' }))
// handle SSR requests
app.all(
'*',
createRequestHandler({
build: vite
? () => vite.ssrLoadModule('virtual:remix/server-build')
: await import('./build/server/index.js'),
}),
)
const port = 3000
app.listen(port, '0.0.0.0', () => {
if (process.env.NODE_ENV === 'production') {
console.log('http://localhost:' + port)
} else {
// since we're using a custom server, emulate what vite dev server prints
const elapsed = Date.now() - start
console.log(
` ${chalk.greenBright.bold('VITE')} ${chalk.green(
`v${viteVersion}`,
)} ${chalk.blueBright.bold('Remix')} ${chalk.blue(
`v${remixVersion}`,
)} ready in ${chalk.bold(elapsed)} ms`,
)
console.log()
console.log(
` ${chalk.greenBright.bold('➜')} ${chalk.bold('Local:')} ${chalk.cyan(
'http://localhost:' + port,
)}`,
)
console.log()
}
})