-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
79 lines (59 loc) · 1.76 KB
/
server.js
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
67
68
69
70
71
72
73
74
75
76
77
78
'use strict';
var fs = require('fs'),
path = require('path'),
express = require('express'),
extend = require('extend'),
prerender = require('prerender-node'),
angularProxy = require('angular-html5-proxy');
var env = process.env.NODE_ENV;
console.log('Starting angular-simple-server');
console.log('NODE_ENV: %s', env);
// Configurations
var localConfig = {};
if (fs.existsSync(__dirname + '/config.js')) {
localConfig = require('./config')[env];
if (!localConfig) {
console.warn(
'config.js file exists, but cannot find config for env \'%s\'. ' +
'Is this what you meant?', env);
localConfig = {};
}
}
var config = extend(true, {
NG_PRERENDER_SERVICE_URL : process.env.NG_PRERENDER_SERVICE_URL || '',
NG_PROXY_TARGET : process.env.NG_PROXY_TARGET || 'http://0.0.0.0:9000',
NG_SERVER_PORT : process.env.NG_SERVER_PORT || 8000
}, localConfig);
console.log('Configurations:');
Object.keys(config).forEach(function (key) {
console.log(' %s: %s', key, config[key] || 'N/A');
});
var app = express();
var extensionFile = 'extension.js',
extensionPath = path.resolve(__dirname, extensionFile),
extension;
try {
extension = require(extensionPath);
}
catch(e) {
console.error(e.stack);
console.error('Load extension failed. Cannot require \'%s\'', extensionFile);
process.exit(1);
}
extension(app, config);
// SEO support
if (config.NG_PRERENDER_SERVICE_URL) {
prerender.set('prerenderServiceUrl', config.NG_PRERENDER_SERVICE_URL);
app.use(prerender);
}
// Proxy to origin cdn server
app.use(angularProxy({
target: config.NG_PROXY_TARGET
}));
app.listen(config.NG_SERVER_PORT, function (err) {
if (err) {
console.error(err);
return;
}
console.log('Server running on %d', config.NG_SERVER_PORT);
});