Skip to content

Commit ef837ff

Browse files
author
hongqi.gan
committed
feat: 增加处理耗时反馈;增加内容压缩;调整中间件顺序
1 parent e252708 commit ef837ff

File tree

3 files changed

+78
-53
lines changed

3 files changed

+78
-53
lines changed

lib/commands/server.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var _keys2 = _interopRequireDefault(_keys);
1919
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2020

2121
var connect = require('connect'),
22+
compression = require('compression'),
2223
fs = require('fs'),
2324
http = require('http'),
2425
https = require('https'),
@@ -55,6 +56,8 @@ exports.setOptions = function (optimist) {
5556
optimist.describe('v', '显示详细编译信息');
5657
optimist.alias('m', 'middlewares');
5758
optimist.describe('m', '加载项目中间件');
59+
optimist.alias('c', 'compress');
60+
optimist.describe('c', '启用响应压缩');
5861
};
5962

6063
exports.run = function (options) {
@@ -65,7 +68,8 @@ exports.run = function (options) {
6568
hot = options.hot === 'false' ? false : true,
6669
middlewares = options.mw || options.middlewares,
6770
isHttps = options.s || options.https,
68-
port = options.p || options.port || 80;
71+
port = options.p || options.port || 80,
72+
useCompress = options.c || options.compress;
6973

7074
var middlewareCache = {},
7175
promiseCache = {},
@@ -82,42 +86,21 @@ exports.run = function (options) {
8286
var usingHotServer = false;
8387
var dateFormat = 'HH:mm:ss';
8488

85-
if (middlewares) {
86-
middlewares.split('|').forEach(function (proName) {
87-
var pro = Manager.getProject(sysPath.join(cwd, proName));
88-
if (pro.check() && Array.isArray(pro.middlewares)) {
89-
pro.middlewares.forEach(function (mw) {
90-
return app.use(mw);
91-
});
92-
}
93-
});
94-
}
95-
96-
// a simple middleware
97-
app.use(favicon(sysPath.join(__dirname, '../../static/imgs/favicon.ico')));
98-
99-
// 预处理
100-
app.use(function (req, res, next) {
101-
var extName = sysPath.extname(req.url);
102-
extName === '.js' && res.setHeader('Content-Type', 'text/javascript; charset=UTF-8');
103-
extName === '.css' && res.setHeader('Content-Type', 'text/css; charset=UTF-8');
104-
res.setHeader('Access-Control-Allow-Origin', '*');
105-
next();
106-
});
107-
10889
// logger
10990
app.use(function (req, res, next) {
11091
var end = res.end;
11192
req._startTime = new Date();
11293

11394
res.end = function (chunk, encoding) {
114-
res.end = end;
115-
res.end(chunk, encoding);
95+
// res.end = end;
96+
end.call(res, chunk, encoding, function () {
97+
log(req.url + ' cost ' + (Date.now() - req._startTime));
98+
});
11699

117100
var isNotMap = sysPath.extname(req.url) !== '.map';
118101
var isNotHotUpdate = req.url.indexOf('hot-update') === -1;
119102
if (isNotMap && isNotHotUpdate) {
120-
var format = '%date %status %method %url %contentLength';
103+
var format = '%date %status %method %url %contentLength %cost';
121104

122105
var parseResult = parse(req, res, format);
123106
return process.nextTick(function () {
@@ -155,6 +138,7 @@ exports.run = function (options) {
155138
format = format.replace(/%url/g, decodeURI(req.originalUrl));
156139
format = format.replace(/%status/g, String(res.statusCode)[statusColor]);
157140
format = format.replace(/%contentLength/g, contentLength.grey);
141+
format = format.replace(/%cost/g, Date.now() - req._startTime);
158142

159143
return {
160144
message: format,
@@ -165,6 +149,34 @@ exports.run = function (options) {
165149
return next();
166150
});
167151

152+
// use compression
153+
if (useCompress) {
154+
app.use(compression());
155+
}
156+
157+
if (middlewares) {
158+
middlewares.split('|').forEach(function (proName) {
159+
var pro = Manager.getProject(sysPath.join(cwd, proName));
160+
if (pro.check() && Array.isArray(pro.middlewares)) {
161+
pro.middlewares.forEach(function (mw) {
162+
return app.use(mw);
163+
});
164+
}
165+
});
166+
}
167+
168+
// a simple middleware
169+
app.use(favicon(sysPath.join(__dirname, '../../static/imgs/favicon.ico')));
170+
171+
// 预处理
172+
app.use(function (req, res, next) {
173+
var extName = sysPath.extname(req.url);
174+
extName === '.js' && res.setHeader('Content-Type', 'text/javascript; charset=UTF-8');
175+
extName === '.css' && res.setHeader('Content-Type', 'text/css; charset=UTF-8');
176+
res.setHeader('Access-Control-Allow-Origin', '*');
177+
next();
178+
});
179+
168180
// 在package.json中配置别名
169181
// 当按照别名访问的时候转到对应的Project的目录
170182
app.use(function (req, res, next) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"cli-cursor": "^1.0.2",
2525
"cli-spinners": "^0.3.0",
2626
"colors": "^1.1.2",
27+
"compression": "^1.7.2",
2728
"compute-cluster": "0.0.9",
2829
"connect": "^3.4.1",
2930
"css-loader": "^0.23.1",
@@ -108,4 +109,4 @@
108109
"helpMessage": ""
109110
}
110111
}
111-
}
112+
}

src/commands/server.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const connect = require('connect'),
4+
compression = require('compression'),
45
fs = require('fs'),
56
http = require('http'),
67
https = require('https'),
@@ -37,6 +38,8 @@ exports.setOptions = (optimist) => {
3738
optimist.describe('v', '显示详细编译信息');
3839
optimist.alias('m', 'middlewares');
3940
optimist.describe('m', '加载项目中间件');
41+
optimist.alias('c', 'compress');
42+
optimist.describe('c', '启用响应压缩');
4043
};
4144

4245
exports.run = (options) => {
@@ -47,7 +50,8 @@ exports.run = (options) => {
4750
hot = options.hot === 'false' ? false : true,
4851
middlewares = options.mw || options.middlewares,
4952
isHttps = options.s || options.https,
50-
port = options.p || options.port || 80;
53+
port = options.p || options.port || 80,
54+
useCompress = options.c || options.compress;
5155

5256
let middlewareCache = {},
5357
promiseCache = {},
@@ -64,40 +68,21 @@ exports.run = (options) => {
6468
let usingHotServer = false;
6569
const dateFormat = 'HH:mm:ss';
6670

67-
if (middlewares) {
68-
middlewares.split('|').forEach((proName) => {
69-
let pro = Manager.getProject(sysPath.join(cwd, proName));
70-
if (pro.check() && Array.isArray(pro.middlewares)) {
71-
pro.middlewares.forEach((mw) => app.use(mw));
72-
}
73-
});
74-
}
75-
76-
// a simple middleware
77-
app.use(favicon(sysPath.join(__dirname, '../../static/imgs/favicon.ico')));
78-
79-
// 预处理
80-
app.use((req, res, next) => {
81-
const extName = sysPath.extname(req.url);
82-
extName === '.js' && res.setHeader('Content-Type', 'text/javascript; charset=UTF-8');
83-
extName === '.css' && res.setHeader('Content-Type', 'text/css; charset=UTF-8');
84-
res.setHeader('Access-Control-Allow-Origin', '*');
85-
next();
86-
});
87-
8871
// logger
8972
app.use((req, res, next) => {
9073
const end = res.end;
9174
req._startTime = new Date;
9275

9376
res.end = (chunk, encoding) => {
94-
res.end = end;
95-
res.end(chunk, encoding);
77+
// res.end = end;
78+
end.call(res, chunk, encoding, function() {
79+
log(`${req.url} cost ${Date.now() - req._startTime}`);
80+
});
9681

9782
const isNotMap = sysPath.extname(req.url) !== '.map';
9883
const isNotHotUpdate = req.url.indexOf('hot-update') === -1;
9984
if(isNotMap && isNotHotUpdate) {
100-
let format = '%date %status %method %url %contentLength';
85+
let format = '%date %status %method %url %contentLength %cost';
10186

10287
const parseResult = parse(req, res, format);
10388
return process.nextTick(() => {
@@ -137,6 +122,7 @@ exports.run = (options) => {
137122
format = format.replace(/%url/g, decodeURI(req.originalUrl));
138123
format = format.replace(/%status/g, String(res.statusCode)[statusColor]);
139124
format = format.replace(/%contentLength/g, contentLength.grey);
125+
format = format.replace(/%cost/g, Date.now() - req._startTime);
140126

141127
return {
142128
message: format,
@@ -147,6 +133,32 @@ exports.run = (options) => {
147133
return next();
148134
});
149135

136+
// use compression
137+
if(useCompress) {
138+
app.use(compression());
139+
}
140+
141+
if (middlewares) {
142+
middlewares.split('|').forEach((proName) => {
143+
let pro = Manager.getProject(sysPath.join(cwd, proName));
144+
if (pro.check() && Array.isArray(pro.middlewares)) {
145+
pro.middlewares.forEach((mw) => app.use(mw));
146+
}
147+
});
148+
}
149+
150+
// a simple middleware
151+
app.use(favicon(sysPath.join(__dirname, '../../static/imgs/favicon.ico')));
152+
153+
// 预处理
154+
app.use((req, res, next) => {
155+
const extName = sysPath.extname(req.url);
156+
extName === '.js' && res.setHeader('Content-Type', 'text/javascript; charset=UTF-8');
157+
extName === '.css' && res.setHeader('Content-Type', 'text/css; charset=UTF-8');
158+
res.setHeader('Access-Control-Allow-Origin', '*');
159+
next();
160+
});
161+
150162
// 在package.json中配置别名
151163
// 当按照别名访问的时候转到对应的Project的目录
152164
app.use((req, res, next) => {

0 commit comments

Comments
 (0)