2016-11-03 14:11:36 +01:00
|
|
|
// the polyfills must be one of the first things imported in node.js.
|
|
|
|
// The only modules to be imported higher - node modules with es6-promise 3.x or other Promise polyfill dependency
|
|
|
|
// (rule of thumb: do it if you have zone.js exception that it has been overwritten)
|
|
|
|
// if you are including modules that modify Promise, such as NewRelic,, you must include them before polyfills
|
2016-09-22 15:54:26 +02:00
|
|
|
import 'angular2-universal-polyfills';
|
2016-11-22 14:36:12 +01:00
|
|
|
import 'ts-helpers';
|
|
|
|
import './__workaround.node'; // temporary until 2.1.1 things are patched in Core
|
2016-11-03 14:11:36 +01:00
|
|
|
|
2016-09-22 15:54:26 +02:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as express from 'express';
|
|
|
|
import * as bodyParser from 'body-parser';
|
|
|
|
import * as cookieParser from 'cookie-parser';
|
2016-11-29 11:42:17 +01:00
|
|
|
import * as morgan from 'morgan';
|
2016-11-22 14:36:12 +01:00
|
|
|
import * as compression from 'compression';
|
2016-09-22 15:54:26 +02:00
|
|
|
|
|
|
|
// Angular 2
|
|
|
|
import { enableProdMode } from '@angular/core';
|
|
|
|
// Angular 2 Universal
|
|
|
|
import { createEngine } from 'angular2-express-engine';
|
|
|
|
|
|
|
|
// App
|
2016-11-22 14:36:12 +01:00
|
|
|
import { MainModule } from './node.module';
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
import { routes } from './server.routes';
|
2016-09-22 15:54:26 +02:00
|
|
|
|
|
|
|
// enable prod for faster renders
|
|
|
|
enableProdMode();
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
const ROOT = path.join(path.resolve(__dirname, '..'));
|
|
|
|
|
|
|
|
// Express View
|
2016-11-03 14:11:36 +01:00
|
|
|
app.engine('.html', createEngine({
|
|
|
|
ngModule: MainModule,
|
|
|
|
providers: [
|
2016-11-22 14:36:12 +01:00
|
|
|
// use only if you have shared state between users
|
|
|
|
// { provide: 'LRU', useFactory: () => new LRU(10) }
|
|
|
|
|
2016-11-03 14:11:36 +01:00
|
|
|
// stateless providers only since it's shared
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
app.set('port', process.env.PORT || 3000);
|
2016-09-22 15:54:26 +02:00
|
|
|
app.set('views', __dirname);
|
|
|
|
app.set('view engine', 'html');
|
2016-11-22 14:36:12 +01:00
|
|
|
app.set('json spaces', 2);
|
2016-09-22 15:54:26 +02:00
|
|
|
|
|
|
|
app.use(cookieParser('Angular 2 Universal'));
|
|
|
|
app.use(bodyParser.json());
|
2016-11-22 14:36:12 +01:00
|
|
|
app.use(compression());
|
2016-09-22 15:54:26 +02:00
|
|
|
|
2016-11-29 11:42:17 +01:00
|
|
|
app.use(morgan('dev'));
|
|
|
|
|
2016-11-22 14:36:12 +01:00
|
|
|
function cacheControl(req, res, next) {
|
|
|
|
// instruct browser to revalidate in 60 seconds
|
|
|
|
res.header('Cache-Control', 'max-age=60');
|
|
|
|
next();
|
|
|
|
}
|
2016-09-22 15:54:26 +02:00
|
|
|
// Serve static files
|
2016-11-22 14:36:12 +01:00
|
|
|
app.use('/assets', cacheControl, express.static(path.join(__dirname, 'assets'), {maxAge: 30}));
|
|
|
|
app.use(cacheControl, express.static(path.join(ROOT, 'dist/client'), {index: false}));
|
2016-09-22 15:54:26 +02:00
|
|
|
|
2016-11-22 14:36:12 +01:00
|
|
|
//
|
|
|
|
/////////////////////////
|
|
|
|
// ** Example API
|
|
|
|
// Notice API should be in aseparate process
|
|
|
|
import { serverApi, createTodoApi } from './backend/api';
|
2016-09-22 15:54:26 +02:00
|
|
|
// Our API for demos only
|
|
|
|
app.get('/data.json', serverApi);
|
2016-11-22 14:36:12 +01:00
|
|
|
app.use('/api', createTodoApi());
|
2016-09-22 15:54:26 +02:00
|
|
|
|
|
|
|
function ngApp(req, res) {
|
|
|
|
res.render('index', {
|
|
|
|
req,
|
|
|
|
res,
|
2016-11-22 14:36:12 +01:00
|
|
|
// time: true, // use this to determine what part of your app is slow only in development
|
2016-09-22 15:54:26 +02:00
|
|
|
preboot: false,
|
|
|
|
baseUrl: '/',
|
|
|
|
requestUrl: req.originalUrl,
|
2016-11-22 14:36:12 +01:00
|
|
|
originUrl: `http://localhost:${ app.get('port') }`
|
2016-09-22 15:54:26 +02:00
|
|
|
});
|
|
|
|
}
|
2016-11-22 14:36:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* use universal for specific routes
|
|
|
|
*/
|
2016-09-22 15:54:26 +02:00
|
|
|
app.get('/', ngApp);
|
2016-11-22 14:36:12 +01:00
|
|
|
routes.forEach(route => {
|
|
|
|
app.get(`/${route}`, ngApp);
|
|
|
|
app.get(`/${route}/*`, ngApp);
|
|
|
|
});
|
|
|
|
app.get('*', ngApp);
|
|
|
|
/*app.get('*', function(req, res) {
|
2016-10-12 15:43:18 +02:00
|
|
|
res.setHeader('Content-Type', 'application/json');
|
2016-11-22 14:36:12 +01:00
|
|
|
var pojo = { status: 404, message: 'No Content' };
|
2016-10-12 15:43:18 +02:00
|
|
|
var json = JSON.stringify(pojo, null, 2);
|
2016-11-22 14:36:12 +01:00
|
|
|
res.status(404).send(json);
|
2016-10-12 15:43:18 +02:00
|
|
|
});
|
2016-11-22 14:36:12 +01:00
|
|
|
*/
|
2016-09-22 15:54:26 +02:00
|
|
|
// Server
|
2016-11-22 14:36:12 +01:00
|
|
|
let server = app.listen(app.get('port'), () => {
|
2016-09-22 15:54:26 +02:00
|
|
|
console.log(`Listening on: http://localhost:${server.address().port}`);
|
|
|
|
});
|