/* 我的实现思路: */
// like-express.js
const http = require("http");
const slice = Array.prototype.slice;
class Express {
constructor() {
this.stacks = [];
}
_isMatch(req, stack) {
if (req.url === "/favicon.ico") {
return false;
}
if (req.url.indexOf(stack.path) === 0) {
if (req.method.toLowerCase() === stack.method || stack.method === "all") {
return true;
}
}
return false;
}
_resgister(method, args) {
if (typeof args[0] === "string") {
this.stacks.push({
path: args[0],
middleware: slice.call(args, 1),
method
});
} else {
this.stacks.push({
path: "/",
middleware: slice.call.call(args),
method
});
}
}
use() {
this._resgister("all", arguments);
}
get() {
this._resgister("get", arguments);
}
post() {
this._resgister("post", arguments);
}
_trigger(req, res) {
const finalStacks = [];
this.stacks.forEach(stack => {
if (this._isMatch(req, stack)) {
finalStacks.push(...stack.middleware);
}
});
const next = () => {
const cb = finalStacks.shift();
if (cb) {
cb(req, res, next);
}
};
next();
}
_serverHandle(req, res) {
res.json = data => {
res.setHeader("content-type", "application/json");
res.end(JSON.stringify(data));
};
this._trigger(req, res);
}
listen() {
// 利用箭头函数在定义时绑定
const server = http.createServer((req, res) => {
// console.log("this === http", this === http); // false
// console.log("this intanceof Express", this instanceof Express); // true
this._serverHandle(req, res);
});
server.listen(...arguments);
}
}
module.exports = function() {
return new Express();
};
// test.js
const expressLike = require("./like-express");
const app = expressLike();
app.use("/", (req, res, next) => {
console.log(1, "use 开始");
next();
console.log(1, "use 结束");
res.json({
say: "Hello World"
});
});
app.get("/test", (req, res, next) => {
console.log(2, "get 开始");
next();
console.log(2, "get 结束");
});
app.post("/test", (req, res, next) => {
console.log(3, "post 开始");
next();
console.log(3, "post 结束");
});
app.use("/test", (req, res, next) => {
console.log(4, "use 开始");
next();
console.log(4, "use 结束");
});
app.listen("1314", () => {
console.log("server is running on PORT 1314");
});
测试链接: `http://localhost:1314/test`, 输出:
1 'use 开始'
2 'get 开始'
4 'use 开始'
4 'use 结束'
2 'get 结束'
1 'use 结束'