先看使用
const express = require('./express');
const app = express();
app.get('/',function (req,res){
res.end('/')
})
app.get('/hello',function (req,res){
res.end('/hello');
})
app.listen(3000,function () {
console.log('server start 3000');
})
两个功能
- 执行
listen方法时创建服务 - 访问方法符合时,访问对应路径,执行相应回调;均不匹配时,返回固定 404 信息;
实现思路
注意到express是一个函数,其返回值是一个具有listen、get方法的对象,我们可以在express的入口进行定义,从而目光转向对listen、get方法的实现了
-
listen方法就是对原生的http模块的一个封装,我们只要在执行时利用原生 node 模块http创建一个服务就可以了 -
get方法和【均不匹配兼容】其实是一个路由功能,目前可以先简单的用一个队列去实现,每次执行get等路由方法,就将路径和对应处理函数入队列,然后在请求来时进行遍历匹配即可。至于 404 兼容,我们可以在初始化时就存入一个处理函数,这样当所有都没有匹配上时就执行即可
具体实现
const http = require('http')
const url = require('url')
function createApplication() {
const router = [
{
path: '*',
method: '*',
handler(req,res){
res.end(`Cannot ${req.method} ${req.url}`)
}
}
]
return {
get(path,handler){
router.push({
path,
method: 'get',
handler
})
},
listen(port,cb){
let server = http.createServer(function (req,res) {
let {
pathname
} = url.parse(req.url); // 获取请求的路径
let requireMethod = req.method.toLowerCase();
for (let index = 1; index < router.length; index++) {
const {method,path,handler} = router[index];
if(pathname === path && requireMethod === method){
return handler(req, res);
}
}
return router[0].handler(req,res);
})
server.listen(...arguments)
}
}
}
module.exports = createApplication
以上就是express提供http服务功能实现示例的详细内容,更多关于express提供http服务的资料请关注本站其它相关文章!