-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
66 lines (52 loc) · 1.48 KB
/
index.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
import * as path from 'path';
import * as express from 'express';
import * as bodyParser from 'body-parser';
// Angular 2 Universal
import 'angular2-universal/polyfills';
import {
provide,
enableProdMode,
expressEngine,
REQUEST_URL,
ORIGIN_URL,
BASE_URL,
NODE_ROUTER_PROVIDERS,
NODE_HTTP_PROVIDERS,
ExpressEngineConfig
} from 'angular2-universal';
// replace this line with your Angular 2 root component
import {App} from './app/app';
const app = express();
const ROOT = path.join(path.resolve(__dirname, '..'));
const port = 6565;
enableProdMode();
// Express View
app.engine('.html', expressEngine);
app.set('views', __dirname);
app.set('view engine', 'html');
app.use(bodyParser.json());
function ngApp(req: any, res: any) {
let baseUrl = '/';
let url = req.originalUrl || '/';
let config: ExpressEngineConfig = {
directives: [App],
platformProviders: [
provide(ORIGIN_URL, { useValue: `http://localhost:${port}` }),
provide(BASE_URL, { useValue: baseUrl }),
],
providers: [
provide(REQUEST_URL, { useValue: url }),
NODE_ROUTER_PROVIDERS,
NODE_HTTP_PROVIDERS,
],
async: true,
preboot: { appRoot: 'app' }
};
console.log('render index');
res.render('index', config);
}
app.use(express.static(ROOT, { index: false }));
app.use('*', ngApp);
app.listen(port, () => {
console.log(`Listening on: http://localhost:${port}`);
});