const http = require('http')

class LikeExpress {
constructor() {
this.middleList = []
this.routes = {
all: [],
get: [],
post: []
}
}
// 处理参数
register(path) {
const info = {}
const slice = Array.prototype.slice
if (typeof path === 'string') {
info.path = path
info.stack = slice.call(arguments, 1)
} else {
info.path = '/'
info.stack = slice.call(arguments, 0)
}
return info
}
use() {
const info = this.register.apply(this, arguments)
this.routes.all.push(info)
}
get() {
const info = this.register.apply(this, arguments)
this.routes.get.push(info)
}
post() {
const info = this.register.apply(this, arguments)
this.routes.post.push(info)
}
match(url, method) {
let stack = []
if (url === 'favicon.ico') {
return stack
}
let curRoutes = []
curRoutes = curRoutes.concat(this.routes.all).concat(this.routes[method])
curRoutes.forEach(route => {
if (url.indexOf(route.path) === 0) {
stack = stack.concat(route.stack)
}
})
return stack
}
handle(list, req, res) {
const next = () => {
const middware = list.shift()
if (middware) {
middware(req, res, next)
}
}
next()
}
callback() {
return (req, res) => {
res.json = data => {
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(data))
}
const url = req.url
const method = req.method.toLowerCase() const resultList = this.match(url, method)
this.handle(resultList, req, res)
}
}
listen(...args) {
const server = http.createServer(this.callback())
server.listen(...args)
}
} module.exports = LikeExpress

最新文章

  1. Android 登录界面与首页的设计
  2. 数据仓库原理<2>:数据仓库系统的体系结构
  3. Write cv::Mat to a file
  4. The world beyond batch: Streaming 101
  5. 右键TXT文件-打开方式-选择默认程序提示 windows无法访问指定设备,路经或文件.您可能没有合适的权限访问这个项目
  6. HTMO DOM部分---小练习;列表之间移动、日期选择、好友选中、滑动效果、滚动条效果、飞入飞出效果。
  7. Jquary获取页面控件的值
  8. 『SQL注入』 User-Agent 手工注入的探测与利用分析
  9. myeclipse 调试JSP页面
  10. web微信开发前期准备最新详细流程
  11. 如何将Windows7系统中“运行”历史记录全部清除
  12. nat的翻译类型(2)--动态nat
  13. Ubuntu16.04 安装NVIDIA显卡驱动
  14. hadoop2.7单节点
  15. 使用通配符和泛型:完成父子类关系的List对象的类型匹配
  16. CentOS下将Python的版本升级为3.x
  17. crontab使用说明及例子程序
  18. Oozie-coordinator调度
  19. log4cpp简单使用及踩到的坑
  20. 使用switchPage.js插件jQuery全屏滚动翻页

热门文章

  1. docker安装指定版本nexus3
  2. windwos源码安装mysql
  3. 查询系统table条数
  4. 利用express-session插件实现nodejs中登录状态的保存
  5. Spring @Autowired 注入为 null
  6. Gulp 给所有静态文件引用加版本号
  7. MSP---助力企业轻松上云
  8. Spring对于事务的控制@Transactional注解详解
  9. Angular使用操作事件指令ng-click传多个参数示例
  10. 学习笔记之DBeaver