-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.ts
139 lines (116 loc) · 3.83 KB
/
server.ts
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
135
136
137
138
139
import path from "path"
import type { RequestHandler } from "@remix-run/express"
import { createRequestHandler } from "@remix-run/express"
import type { ServerBuild } from "@remix-run/node"
import { broadcastDevReady, installGlobals } from "@remix-run/node"
import compression from "compression"
import express from "express"
import morgan from "morgan"
import getPort, { portNumbers } from "get-port"
import chalk from "chalk"
import { ip as ipAddress } from "address"
import closeWithGrace from "close-with-grace"
import { fileURLToPath } from "url"
installGlobals()
const BUILD_PATH = "./build/index.js"
const WATCH_PATH = "./build/version.txt"
/**
* Initial build
* @type {ServerBuild}
*/
const build = await import(BUILD_PATH)
let devBuild = build
const app = express()
app.use(compression())
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
app.disable("x-powered-by")
// Remix fingerprints its assets so we can cache forever.
app.use(
"/build",
express.static("public/build", { immutable: true, maxAge: "1y" }),
)
// Everything else (like favicon.ico) is cached for an hour. You may want to be
// more aggressive with this caching.
app.use(
express.static("public", {
maxAge: "1h",
redirect: false,
}),
)
app.use(morgan("tiny"))
function getRequestHandler(build: ServerBuild): RequestHandler {
return createRequestHandler({
build,
mode: process.env.NODE_ENV,
})
}
app.all(
"*",
process.env.NODE_ENV === "development"
? (...args) => getRequestHandler(devBuild)(...args)
: getRequestHandler(build),
)
const desiredPort = Number(process.env.PORT || 3000)
const portToUse = await getPort({
port: portNumbers(desiredPort, desiredPort + 100),
})
const server = app.listen(portToUse, () => {
const addy = server.address()
const portUsed =
desiredPort === portToUse
? desiredPort
: addy && typeof addy === "object"
? addy.port
: 0
if (portUsed !== desiredPort) {
console.warn(
chalk.yellow(
`⚠️ Port ${desiredPort} is not available, using ${portUsed} instead.`,
),
)
}
console.log(`🚀 We have liftoff!`)
const localUrl = `http://localhost:${portUsed}`
let lanUrl: string | null = null
const localIp = ipAddress() ?? "Unknown"
// Check if the address is a private ip
// https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces
// https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-dev-utils/WebpackDevServerUtils.js#LL48C9-L54C10
if (/^10[.]|^172[.](1[6-9]|2[0-9]|3[0-1])[.]|^192[.]168[.]/.test(localIp)) {
lanUrl = `http://${localIp}:${portUsed}`
}
console.log(
`
${chalk.bold("Local:")} ${chalk.cyan(localUrl)}
${lanUrl ? `${chalk.bold("On Your Network:")} ${chalk.cyan(lanUrl)}` : ""}
${chalk.bold("Press Ctrl+C to stop")}
`.trim(),
)
if (process.env.NODE_ENV === "development") {
broadcastDevReady(build)
}
})
closeWithGrace(async ({ err }) => {
if (err) {
console.error(chalk.red(err?.message))
}
await new Promise((resolve, reject) => {
server.close((e) => (e ? reject(e) : resolve("ok")))
})
})
// during dev, we'll keep the build module up to date with the changes
if (process.env.NODE_ENV === "development") {
async function reloadBuild() {
devBuild = await import(`${BUILD_PATH}?update=${Date.now()}`)
broadcastDevReady(devBuild)
}
// We'll import chokidar here so doesn't get bundled in production.
const chokidar = await import("chokidar")
const dirname = path.dirname(fileURLToPath(import.meta.url))
const watchPath = path.join(dirname, WATCH_PATH).replace(/\\/g, "/")
const buildWatcher = chokidar
.watch(watchPath, { ignoreInitial: true })
.on("add", reloadBuild)
.on("change", reloadBuild)
closeWithGrace(() => buildWatcher.close())
}