Skip to content

Commit afd868b

Browse files
committed
feat(server.js): applyMiddleware API升级,现在允许用户调用多次绑定多个Middleware,它们将会按照绑定顺序依次执行。
1 parent eadd1c9 commit afd868b

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

lib/commands/server.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
'use strict';
22

3-
function _defineProperty(obj, key, value) {
4-
if (key in obj) {
5-
Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
6-
} else {
7-
obj[key] = value;
8-
}
9-
return obj;
10-
}
3+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
114

125
var connect = require('connect'),
136
fs = require('fs'),
@@ -135,19 +128,21 @@ exports.run = function (options) {
135128

136129
app.use(function (req, res, next) {
137130
try {
138-
var projectInfo = getProjectInfo(req);
139-
var project = Manager.getProject(projectInfo.projectCwd, { cache: false });
140-
var customMiddlewares = project.config.getMiddlewares();
141-
var _next = () => {
142-
if (customMiddlewares.length === 0) {
143-
next();
144-
} else {
145-
const nextMw = customMiddlewares.shift();
146-
nextMw(req, res, _next);
147-
}
148-
};
149-
150-
_next();
131+
(function () {
132+
var projectInfo = getProjectInfo(req);
133+
var project = Manager.getProject(projectInfo.projectCwd, { cache: false });
134+
var customMiddlewares = project.config.getMiddlewares();
135+
var _next = function _next() {
136+
if (customMiddlewares.length === 0) {
137+
next();
138+
} else {
139+
var nextMw = customMiddlewares.shift();
140+
nextMw(req, res, _next);
141+
}
142+
};
143+
144+
_next();
145+
})();
151146
} catch (e) {
152147
next();
153148
}

src/commands/server.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,17 @@ exports.run = (options) => {
128128
try {
129129
const projectInfo = getProjectInfo(req);
130130
const project = Manager.getProject(projectInfo.projectCwd, { cache: false });
131-
const customMiddleware = project.config.getMiddleware();
131+
const customMiddlewares = project.config.getMiddlewares();
132+
const _next = () => {
133+
if (customMiddlewares.length === 0) {
134+
next();
135+
} else {
136+
const nextMw = customMiddlewares.shift();
137+
nextMw(req, res, _next);
138+
}
139+
};
132140

133-
if (typeof customMiddleware === 'function') {
134-
customMiddleware(req, res, next);
135-
} else {
136-
next();
137-
}
141+
_next();
138142
} catch (e) {
139143
next();
140144
}

0 commit comments

Comments
 (0)