-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
161 lines (134 loc) · 3.57 KB
/
server.js
File metadata and controls
161 lines (134 loc) · 3.57 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* PoliteJS Workspace - Debug Server
*
* // run standard debug server on 8080
* node server.js
*
* // custom port
* node server.js 1234
*
* // serve release folder on 8080
* node server.js -r
*
* // serve release folder on custom port
* node server.js -r 1234
*
*/
var http = require('http');
var path = require('path');
var fs = require('fs');
var extend = require('jqb-extend');
var express = require('express');
var app = express();
var server = http.createServer(app);
var serveStatic = require('serve-static');
var cookieParser = require('cookie-parser');
var compression = require('compression');
var lifeCycle = require('jqb-lifecycle');
// dynamic settings for path and port
var TARGET = null;
var PORT = null;
var RELEASE_MODE = false;
var args = require('yargs').argv;
var config = {};
// try to parse config from arguments json config
if (args.c) {
try {
eval('config = ' + decodeURIComponent(args.c));
} catch(e) {
console.log('Server configuration parsin error:');
console.log(e);
console.log(args.c);
}
config = extend({}, config || {});
// load config from workspace.conf.js
} else {
var userConfig = require(path.join(process.cwd(), 'workspace.conf.js'));
var Workspace = require('./index');
Workspace.init(userConfig);
config = Workspace.getConfig();
// console.log(config);
// console.log("----");
config.server.dev.target = config.target.dev.path;
console.log(config.server.dev);
// process.exit(1);
config = config.server.dev;
}
if (config.target) {
TARGET = config.target;
}
if (config.port) {
PORT = config.port;
}
// direct arguments overrides configuration
if (args.w) {
TARGET = args.w;
}
if (args.p) {
PORT = args.p;
}
// default settings
if (TARGET === null) {
TARGET = 'build/dev';
}
if (PORT === null) {
PORT = '8080';
}
// Settings
var ROOT_DIR = process.cwd();
var PUBLIC_DIR = path.join(ROOT_DIR, '' + TARGET);
// Compress output
if (config.compress) {
app.use(compression());
}
// Parsing
app.use(cookieParser());
// prevent cache
if (!config.isDev) {
app.use(function(req, res, next){
req.connection.setTimeout(500);
res.setHeader('Last-Modified', (new Date()).toUTCString());
req.connection.setTimeout(500);
next();
});
}
// init custom logic
var entryPoint, featuresPath, features;
if (config.entryPoint) {
entryPoint = require(path.join(process.cwd(), config.entryPoint));
entryPoint.init && entryPoint.init(server, app, config);
}
if (config.features) {
featuresPath = path.join(process.cwd(), config.features);
features = fs.readdirSync(featuresPath).filter(function(name) {
return name.indexOf('.') === -1;
}).map(function(name) {
return require(path.join(featuresPath, name));
});
lifeCycle.start(features, server, app, config);
}
if (entryPoint) {
entryPoint.start && entryPoint.start();
}
// static files
app.use(serveStatic(PUBLIC_DIR));
// pushstate support
app.get('*', function(request, response){
response.sendfile(PUBLIC_DIR + '/index.html');
});
// Start
server.listen(PORT);
console.log(' ');
console.log('======= PoliteJS Workspace ========');
console.log('Just open your Chrome and point to:');
console.log('http://localhost:%s', PORT);
console.log('===================================');
// live reload
if (config.livereload !== false) {
console.log(' ');
console.log('LiveReload is enabled!');
console.log(' ');
var livereload = require('livereload');
server = livereload.createServer();
server.watch(PUBLIC_DIR);
}