-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (71 loc) · 1.94 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
import 'dotenv/config'
import { createRequestHandler } from '@remix-run/express'
import chalk from 'chalk'
import closeWithGrace from 'close-with-grace'
import express from 'express'
import getPort, { portNumbers } from 'get-port'
import morgan from 'morgan'
import { printUrls } from './server-utils.js'
const viteDevServer =
process.env.NODE_ENV === 'production'
? null
: await import('vite').then((vite) =>
vite.createServer({
server: { middlewareMode: true },
}),
)
const app = express()
app.use(
morgan('tiny', {
skip:
process.env.NODE_ENV === 'development'
? (req, res) => {
if (
req.url.startsWith('/node_modules') ||
req.url.startsWith('/app') ||
req.url.startsWith('/@') ||
req.url.startsWith('/__')
) {
if (res.statusCode === 200 || res.statusCode === 304) {
return true
}
}
}
: undefined,
}),
)
app.use(
viteDevServer ? viteDevServer.middlewares : express.static('build/client'),
)
const build = viteDevServer
? () => viteDevServer.ssrLoadModule('virtual:remix/server-build')
: await import('./build/server/index.js')
app.all('*', createRequestHandler({ build }))
const desiredPort = Number(process.env.PORT || 3000)
const portToUse = await getPort({
port: portNumbers(desiredPort, desiredPort + 100),
})
const server = app.listen(portToUse, () => {
const addr = server.address()
const portUsed = typeof addr === 'object' ? addr.port : addr
if (portUsed !== desiredPort) {
console.warn(
chalk.yellow(
`⚠️ Port ${desiredPort} is not available, using ${portUsed} instead.`,
),
)
}
console.log(`🚀 App started`)
printUrls(portUsed)
console.log(chalk.bold('Press Ctrl+C to stop'))
})
closeWithGrace(async ({ err }) => {
if (err) {
console.error(chalk.red(err))
console.error(chalk.red(err.stack))
process.exit(1)
}
await new Promise((resolve, reject) => {
server.close((e) => (e ? reject(e) : resolve()))
})
})