forked from steveblue/angular2-rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
103 lines (68 loc) · 1.95 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"use strict";
// server.js
const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const app = express();
const config = require('./ngr.config.js');
const serverConfig = {
dev: require('./server.config.dev.js'),
prod: require('./server.config.prod.js')
};
let env = process.env.NODE_ENV || 'dev';
const port = serverConfig[env].port || process.env.PORT;
const host = serverConfig[env].origin;
let ssl = false;
let canWatch = false;
let server;
process.argv.forEach(function(arg){
if(arg === '--https') {
ssl = true;
}
if (arg.includes('watch')) {
canWatch = arg.split('=')[1].trim() === 'true' ? true : false;
}
});
// Livereload Server Start
let live = function() {
let livereload = require('livereload');
let liveserver = livereload.createServer({
port: 35729
});
liveserver.watch([__dirname + '/'+config.build+'/assets',
__dirname + '/'+config.build+'/src',
__dirname + '/'+config.build+'/style',
__dirname + '/'+config.build+'/*.html',
__dirname + '/'+config.build+'/*.js',
__dirname + '/'+config.build+'/*.css']);
console.log('Livereload available at '+host+':'+35729);
};
// Create Server
if ( env === 'prod' ) {
if ( ssl === true ) {
const options = {
key: fs.readFileSync('./conf/ssl/key.pem'),
cert: fs.readFileSync('./conf/ssl/cert.pem')
};
server = https.createServer(options, app);
} else {
server = http.createServer(app);
}
if (canWatch === true) {
live();
}
}
if ( env === 'dev' ) {
server = http.createServer(app);
if (canWatch === true) {
live();
}
}
// Express Middleware
// Load Modules
const routes = require('./router')(app);
// Server Start
server.listen(port);
console.log('Express available at '+host+':'+port);
module.exports = app;