Project

General

Profile

1
// the polyfills must be one of the first things imported in node.js.
2
// The only modules to be imported higher - node modules with es6-promise 3.x or other Promise polyfill dependency
3
// (rule of thumb: do it if you have zone.js exception that it has been overwritten)
4
// if you are including modules that modify Promise, such as NewRelic,, you must include them before polyfills
5
import 'angular2-universal-polyfills';
6
import 'ts-helpers';
7
import './__workaround.node'; // temporary until 2.1.1 things are patched in Core
8

    
9
import * as path from 'path';
10
import * as express from 'express';
11
import * as bodyParser from 'body-parser';
12
import * as cookieParser from 'cookie-parser';
13
import * as compression from 'compression';
14

    
15
// Angular 2
16
import { enableProdMode } from '@angular/core';
17
// Angular 2 Universal
18
import { createEngine } from 'angular2-express-engine';
19

    
20
// App
21
import { MainModuleNgFactory } from './node.module.ngfactory';
22

    
23
// Routes
24
import { routes } from './server.routes';
25

    
26
// enable prod for faster renders
27
enableProdMode();
28

    
29
const app = express();
30
const ROOT = path.join(path.resolve(__dirname, '..'));
31

    
32
// Express View
33
app.engine('.html', createEngine({
34
  precompile: false, // this needs to be false when using ngFactory
35
  ngModule: MainModuleNgFactory,
36
  providers: [
37
    // use only if you have shared state between users
38
    // { provide: 'LRU', useFactory: () => new LRU(10) }
39

    
40
    // stateless providers only since it's shared
41
  ]
42
}));
43
app.set('port', process.env.PORT || 3000);
44
app.set('views', __dirname);
45
app.set('view engine', 'html');
46
app.set('json spaces', 2);
47

    
48
app.use(cookieParser('Angular 2 Universal'));
49
app.use(bodyParser.json());
50
app.use(compression());
51

    
52
function cacheControl(req, res, next) {
53
  // instruct browser to revalidate in 60 seconds
54
  res.header('Cache-Control', 'max-age=60');
55
  next();
56
}
57
// Serve static files
58
app.use('/assets', cacheControl, express.static(path.join(__dirname, 'assets'), {maxAge: 30}));
59
app.use(cacheControl, express.static(path.join(ROOT, 'dist/client'), {index: false}));
60

    
61
//
62
/////////////////////////
63
// ** Example API
64
// Notice API should be in aseparate process
65
import { serverApi, createTodoApi } from './backend/api';
66
// Our API for demos only
67
app.get('/data.json', serverApi);
68
app.use('/api', createTodoApi());
69

    
70
function ngApp(req, res) {
71
  res.render('index', {
72
    req,
73
    res,
74
    // time: true, // use this to determine what part of your app is slow only in development
75
    preboot: false,
76
    baseUrl: '/',
77
    requestUrl: req.originalUrl,
78
    originUrl: `http://localhost:${ app.get('port') }`
79
  });
80
}
81

    
82
/**
83
 * use universal for specific routes
84
 */
85
app.get('/', ngApp);
86
routes.forEach(route => {
87
  app.get(`/${route}`, ngApp);
88
  app.get(`/${route}/*`, ngApp);
89
});
90

    
91

    
92
app.get('*', function(req, res) {
93
  res.setHeader('Content-Type', 'application/json');
94
  var pojo = { status: 404, message: 'No Content' };
95
  var json = JSON.stringify(pojo, null, 2);
96
  res.status(404).send(json);
97
});
98

    
99
// Server
100
let server = app.listen(app.get('port'), () => {
101
  console.log(`Listening on: http://localhost:${server.address().port}`);
102
});
(9-9/12)