-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.ts
115 lines (103 loc) · 2.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
import { createRequestHandler } from "@remix-run/express";
import compression from "compression";
import express, { type Request, type Response, type Express } from "express";
import fs from "fs";
import https from "https";
import morgan from "morgan";
import WebSocket from "ws";
type User = {
id: string;
name: string;
};
declare global {
namespace Express {
export interface Request {
user?: User;
}
}
}
export function createApp(
buildPath: string,
mode = "production",
publicPath = "/build/",
assetsBuildDirectory = "public/build/"
) {
let app = express();
app.disable("x-powered-by");
app.use(function (req, res, next) {
res.setHeader("X-Powered-By", "TS Remix App Server");
next();
});
app.use(compression());
app.use(
publicPath,
express.static(assetsBuildDirectory, { immutable: true, maxAge: "1y" })
);
app.use(express.static("public", { maxAge: "1h" }));
app.use(morgan("tiny"));
// add custom loader context
function getLoadContext(req: Request, _res: Response) {
// this becomes the loader context
return { expressUser: req.user };
}
app.all(
"*",
mode === "production"
? createRequestHandler({
build: require(buildPath),
mode,
getLoadContext,
})
: (req, res, next) => {
// require cache is purged in @remix-run/dev where the file watcher is
purgeRequireCache(buildPath);
let build = require(buildPath);
return createRequestHandler({ build, mode, getLoadContext })(
req,
res,
next
);
}
);
return app;
}
function purgeRequireCache(buildPath: string) {
for (const key in require.cache) {
if (key.startsWith(buildPath)) {
console.log("purging", key);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete require.cache[key];
}
}
}
export function createServer(app: Express, port: number) {
const server = https
.createServer(
// Provide the private and public key to the server by reading each
// file's content with the readFileSync() method.
{
key: fs.readFileSync("server.key"),
cert: fs.readFileSync("server.crt"),
},
app
)
.listen(port, () => {
console.log(`server is running at https://localhost:${port}`);
});
return server;
}
export function createSocketServer(port: number) {
const server = https.createServer(
// Provide the private and public key to the server by reading each
// file's content with the readFileSync() method.
{
key: fs.readFileSync("server.key"),
cert: fs.readFileSync("server.crt"),
}
);
const wss = new WebSocket.Server({ server });
server.listen(port, () => {
console.log(`socket server is running at https://localhost:${port}`);
});
return wss;
}