Project

General

Profile

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('/my-claims', ngApp);
50
app.get('/linking', ngApp);
51
app.get('/demo-up', ngApp);
52
app.get('/up', ngApp);
53
app.get('/bulk-linking', ngApp);
54
app.get('/publication', ngApp);
55
app.get('/project', ngApp);
56
app.get('/person', ngApp);
57
app.get('/dataset', ngApp);
58
app.get('/organization', ngApp);
59
app.get('/deposit', ngApp);
60

    
61
// use indexFile over ngApp only when there is too much load on the server
62
function indexFile(req, res) {
63
  // when there is too much load on the server just send
64
  // the index.html without prerendering for client-only
65
  res.sendFile('/index.html', {root: __dirname});
66
}
67

    
68
app.get('*', function(req, res) {
69
  res.setHeader('Content-Type', 'application/json');
70
  var pojo = { status: 404, message: 'No Content' };
71
  var json = JSON.stringify(pojo, null, 2);
72
  res.status(404).send(json);
73
});
74

    
75
// Server
76
app.listen(3000, () => {
77
  console.log('Listening on: http://localhost:3000');
78
});
(6-6/7)