forked from FrozenPandaz/ng-universal-demo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
153 lines (124 loc) · 4.33 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import express from 'express';
import compression from 'compression';
import {fileURLToPath} from 'node:url';
import {dirname} from 'node:path';
import path from 'path';
import fs from 'fs';
import {createProxyMiddleware} from 'http-proxy-middleware';
import yargs from 'yargs/yargs';
import {hideBin} from 'yargs/helpers';
import {extendConnectUse} from 'nodejs-connect-extensions';
import dotenv from 'dotenv';
import serverMock from './server.mock.cjs';
const consoleLog = console.log;
const consoleError = console.error;
const consoleWarn = console.warn;
const logs = [];
console.log = (message, ...optionalParams) =>
{
logs.push(JSON.stringify(message) + ', ' + optionalParams.map(itm => JSON.stringify(itm)).join(', '));
consoleLog(message, ...optionalParams);
};
console.error = (message, ...optionalParams) =>
{
logs.push('ERROR: ' + JSON.stringify(message) + ', ' + optionalParams.map(itm => JSON.stringify(itm)).join(', '));
consoleError(message, ...optionalParams);
};
console.warn = (message, ...optionalParams) =>
{
logs.push('WARNING: ' + JSON.stringify(message) + ', ' + optionalParams.map(itm => JSON.stringify(itm)).join(', '));
consoleWarn(message, ...optionalParams);
};
async function run()
{
const argv = yargs(hideBin(process.argv)).argv;
const server = express();
server.use(compression());
dotenv.config();
extendConnectUse(server);
const dirName = dirname(fileURLToPath(import.meta.url));
const wwwroot = path.join(dirName, 'wwwroot', 'browser');
const indexHtml = path.join(wwwroot, 'index.html')
const serverPath = path.join(dirName, 'wwwroot', 'server', 'server.mjs');
const proxyUrlFile = path.join(dirName, 'proxyUrl.js');
let proxyUrl = "http://127.0.0.1:8080";
let port = process.env['PORT'] || 8888;
if(fs.existsSync(proxyUrlFile))
{
proxyUrl = (await import('./proxyUrl.js')).default;
}
if(process.env.SERVER_PROXY_HOST)
{
proxyUrl = process.env.SERVER_PROXY_HOST;
}
console.log(`Using proxy url '${proxyUrl}'`);
//start with dev port
if(!!argv.devPort)
{
port = 8880;
}
server.get('/consoleLog', (_, res) => res.send(logs));
//mock rest api
serverMock(server);
function error(err, req, res)
{
if(err.code == "ECONNREFUSED" || err.code == "ECONNRESET")
{
res.writeHead(503,
{
'Content-Type': 'text/plain'
});
res.end('Remote server is offline.');
return;
}
res.writeHead(504,
{
'Content-Type': 'text/plain'
});
res.end('Failed to proxy request.');
}
//proxy special requests to other location
server.use(createProxyMiddleware(['/api'],
{
target: proxyUrl,
ws: true,
secure: false,
changeOrigin: true,
on:
{
error,
},
}));
server.set('view engine', 'html');
server.set('views', wwwroot);
// Serve static files from /browser
server.get('*.*', express.static(wwwroot,
{
maxAge: '1y',
setHeaders: (res, path) =>
{
if (express.static.mime.lookup(path) === 'text/html')
{
// Skip cache on html to load new builds.
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Expires', '-1');
res.setHeader('Pragma', 'no-cache');
}
}
}));
if(fs.existsSync(serverPath) && !argv.devPort)
{
const {applyServerSideRendering} = await import('./wwwroot/server/server.mjs');
applyServerSideRendering(server);
}
else
{
server.get('/*', (_, res) => res.sendFile(indexHtml));
}
//create node.js http server and listen on port
server.listen(port, () =>
{
console.log(`Listening on port ${port} => http://localhost:${port}`);
});
}
run();