-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.ts
More file actions
72 lines (61 loc) · 2.03 KB
/
server.ts
File metadata and controls
72 lines (61 loc) · 2.03 KB
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
import {
AngularNodeAppEngine,
createNodeRequestHandler,
isMainModule,
writeResponseToNodeResponse
} from "@angular/ssr/node";
import express from "express";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { json, urlencoded } from "body-parser";
import { convertIPEmail } from "@wlocalhost/ngx-email-builder-convertor";
const {NODE_ENV, PORT} = process.env;
const isProduction = NODE_ENV === 'production';
// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
server.disable("etag").disable("x-powered-by");
server.use(json({ limit: "1mb" }));
server.use(urlencoded({ limit: "1mb", extended: true }));
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, "../browser");
const appEngine = new AngularNodeAppEngine();
// Example Express Rest API endpoints
server.post("/api/convert", (req, res) => {
res.json(convertIPEmail(req.body, isProduction));
});
server.get(
"*.*",
express.static(browserDistFolder, {
maxAge: "1y"
})
);
server.get("/404", (req, res, next) => {
res.send("Express is serving this server only error");
});
// All regular routes use the Angular engine
server.use("*", async (req, res, next) => {
// const { protocol, originalUrl, baseUrl, headers } = req;
try {
const response = await appEngine.handle(req, { server: "express" });
if (response) {
await writeResponseToNodeResponse(response, res);
} else {
next();
}
} catch (err) {
next(err);
}
});
return server;
}
const server = app();
if (isMainModule(import.meta.url)) {
const port = PORT || 4000;
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
console.warn("Node Express server started");
// This exposes the RequestHandler
export const reqHandler = createNodeRequestHandler(server);