1
|
// the polyfills must be the first thing imported in node.js
|
2
|
import 'angular2-universal/polyfills';
|
3
|
|
4
|
import * as path from 'path';
|
5
|
import * as express from 'express';
|
6
|
import * as bodyParser from 'body-parser';
|
7
|
import * as cookieParser from 'cookie-parser';
|
8
|
|
9
|
// Angular 2
|
10
|
import { enableProdMode } from '@angular/core';
|
11
|
// Angular 2 Universal
|
12
|
import { expressEngine } from 'angular2-universal';
|
13
|
|
14
|
// enable prod for faster renders
|
15
|
enableProdMode();
|
16
|
|
17
|
const app = express();
|
18
|
const ROOT = path.join(path.resolve(__dirname, '..'));
|
19
|
|
20
|
// Express View
|
21
|
app.engine('.html', expressEngine);
|
22
|
app.set('views', __dirname);
|
23
|
app.set('view engine', 'html');
|
24
|
|
25
|
app.use(cookieParser('Angular 2 Universal'));
|
26
|
app.use(bodyParser.json());
|
27
|
|
28
|
// Serve static files
|
29
|
app.use('/assets', express.static(path.join(__dirname, 'assets'), {maxAge: 30}));
|
30
|
app.use(express.static(path.join(ROOT, 'dist/client'), {index: false}));
|
31
|
/* **AK** Add this path so that index.html can see files from node_modules folder (e.g. bootstrap files)*/
|
32
|
app.use(express.static(path.join(ROOT, 'node_modules'), {index: false}));
|
33
|
|
34
|
|
35
|
|
36
|
// import { serverApi } from './backend/api';
|
37
|
// // Our API for demos only
|
38
|
// app.get('/data.json', serverApi);
|
39
|
|
40
|
import { ngApp } from './main.node';
|
41
|
// Routes with html5pushstate
|
42
|
// ensure routes match client-side-app
|
43
|
app.get('/', ngApp);
|
44
|
app.get('/claims', ngApp);
|
45
|
app.get('/claim', ngApp);
|
46
|
app.get('/home', ngApp);
|
47
|
app.get('/search', ngApp);
|
48
|
app.get('/search-publications', ngApp);
|
49
|
app.get('/advanced-search-publications', ngApp);
|
50
|
app.get('/my-claims', ngApp);
|
51
|
app.get('/linking', ngApp);
|
52
|
app.get('/demo-up', ngApp);
|
53
|
app.get('/up', ngApp);
|
54
|
app.get('/bulk-linking', ngApp);
|
55
|
app.get('/publication', ngApp);
|
56
|
app.get('/project', ngApp);
|
57
|
app.get('/person', ngApp);
|
58
|
app.get('/dataset', ngApp);
|
59
|
app.get('/organization', ngApp);
|
60
|
app.get('/deposit', ngApp);
|
61
|
|
62
|
// use indexFile over ngApp only when there is too much load on the server
|
63
|
function indexFile(req, res) {
|
64
|
// when there is too much load on the server just send
|
65
|
// the index.html without prerendering for client-only
|
66
|
res.sendFile('/index.html', {root: __dirname});
|
67
|
}
|
68
|
|
69
|
app.get('*', function(req, res) {
|
70
|
res.setHeader('Content-Type', 'application/json');
|
71
|
var pojo = { status: 404, message: 'No Content' };
|
72
|
var json = JSON.stringify(pojo, null, 2);
|
73
|
res.status(404).send(json);
|
74
|
});
|
75
|
|
76
|
// Server
|
77
|
app.listen(3000, () => {
|
78
|
console.log('Listening on: http://localhost:3000');
|
79
|
});
|