-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts.bak
142 lines (122 loc) · 5.06 KB
/
server.ts.bak
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
import 'zone.js/node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { environment } from 'src/environments/environment';
import { HOST_ID } from 'host';
import { USER_AGENT } from 'user-agent';
console.log('START');
// ssr DOM
const domino = require('domino');
const fs = require('fs');
const mcache = require('memory-cache');
// index from browser build!
const template = fs.readFileSync(environment.serverRenderingPath.localIndex).toString();
// for mock global window by domino
const win = domino.createWindow(template);
// mock
global['window'] = win;
// not implemented property and functions
Object.defineProperty(win.document.body.style, 'transform', {
value: () => {
return {
enumerable: true,
configurable: true,
};
},
});
// mock documnet
global['document'] = win.document;
// othres mock
global['CSS'] = null;
// global['XMLHttpRequest'] = require('xmlhttprequest').XMLHttpRequest;
global['Prism'] = null;
// The Express app is exported so that it can be used by serverless Functions.
export function app() {
const server = express();
//const distFolder = join(process.cwd(), environment.serverRenderingPath.browserFiles);
const browserFolder = environment.serverRenderingPath.browserFiles;
const indexHtml = environment.serverRenderingPath.localIndex;
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
server.engine('html', ngExpressEngine({
bootstrap: AppServerModule,
}));
server.set('view engine', 'html');
server.set('views', browserFolder);
/**
* Cache page handler
* @param duration secondes
* @returns
*/
var cache = (duration: number) => {
return (req, res, next) => {
let key = '__express__' + req.originalUrl || req.url;
if (/\?deleteCache=true/i.test(key)) {
key = key.replace(/\?deleteCache=true/i, '');
mcache.del(key);
}
let cachedBody = mcache.get(key);
if (cachedBody) {
res.send(cachedBody);
return;
} else {
res.sendResponse = res.send;
res.send = (body) => {
mcache.put(key, body, duration * 1000);
res.sendResponse(body);
}
next();
}
}
}
// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// Serve static files from /browser
// all the following paths works
/* const staticFolder2 = __dirname + '/../../../public_html/';
const staticFolder4 = __dirname + '/../browser/';
const staticFolder3 = '/home/gero8821/public_html/';
console.info("staticFolder2 ", staticFolder3, '//// file css exist ? ////', fs.existsSync(staticFolder3+'styles.22a7db0f6abba665215b.css'), '//// js ///', fs.existsSync(staticFolder3+'main-es2015.3ebd9a83e0edfea265b4.js'), '/// img ///', fs.existsSync(staticFolder3+'sleeping-bs/img/guide-chambre-ideale-10-conseils/img1/article/m.jpg'));
console.info("staticFolder3 ", staticFolder3, '//// file css exist ? ////', fs.existsSync(staticFolder3+'styles.22a7db0f6abba665215b.css'), '//// js ///', fs.existsSync(staticFolder3+'main-es2015.3ebd9a83e0edfea265b4.js'), '/// img ///', fs.existsSync(staticFolder3+'sleeping-bs/img/guide-chambre-ideale-10-conseils/img1/article/m.jpg'));
console.error("staticFolder4 ", staticFolder4, '//// file css exist ? ////', fs.existsSync(staticFolder4+'styles.22a7db0f6abba665215b.css'), '//// js ///', fs.existsSync(staticFolder3+'main-es2015.3ebd9a83e0edfea265b4.js'), '/// img ///', fs.existsSync(staticFolder3+'sleeping-bs/img/guide-chambre-ideale-10-conseils/img1/article/m.jpg'));
*/
const abolsutePathPublicHtml = environment.serverRenderingPath.abolsutePathPublicHtml;
server.get('*.*', express.static(abolsutePathPublicHtml, {
maxAge: '1y'
}));
// All regular routes use the Universal engine
// Add cache for 1 year
server.get('*', cache(2147483), (req, res) => {
res.render(indexHtml,
{
req,
providers: [
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
{ provide: HOST_ID, useValue: req.get('host') + req.originalUrl }, // sending host name in provider
{ provide: USER_AGENT, useValue: req.get('User-Agent') },
]
});
});
return server;
}
function run() {
const port = 4000;
console.log("port", port);
// Start up the Node server
const server = app();
server.listen(port, () => {
console.error(`Node Express server listening on http://localhost:${port}`);
});
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('node')) {
console.log('run');
run();
}
export * from './src/main.server';