Nodejs 路由模块封装、封装仿照 express 的路由
- Nodejs 路由模块封装
- 封装仿照 express 的路由
Nodejs 路由模块封装
//model.js
const ejs = require('ejs')
const app = {
login:(req,res)=>{
console.lpg('login')
//res.end('login')
ejs.renderFile('views/form.ejs',{},(err,data)=>{
res.end(data)
})
}
register:(req,res)=>{
console.log('regisiter')
res.end('register')
}
home:(req,res)=>{
console.log('home')
res.end('home')
}
}
module.exports = app
//router.js
const model = require('./model')
model['login']('111','222')
http.createServer((req,res)=>{
let pathname = url.parse(req.url).pathname.replace('/','') //login
if(pathname !== 'facicon.ico'){
try{
model[pathname](req,res)
}catch(err){
model['home'](req,res)
}
}
})
封装仿照 express 的路由
let G = {}
const app = (req,res)=>{
let pathname = url.parse(req.url).pathname
if(pathname.endsWith('/')){
pathname = pathname + '/'
}
/*if(G['login']){
//执行注册的方法
G['login'](req,res)
}*/
if(G(pathname)){
G[pathname](req,res)
}else{
res.end('no router')
}
}
//定义一个get方法
app.get = (string,callback)=>{
if(!string.endsWith('/')){
string = string + '/'
}
if(!string.startsWith('/')){
string = '/' + string
}
// /login/
G[string] = callback
//注册方法
//G['login'] = (req,res)=>{}
}
//执行get方法
app.get('login',(req,res)=>{
console,log('login')
})
setTimeout(()=>{
app('req','res')
},500)
/************************************************/
const http = rewuire('http')
//只要有请求,就会触发app这个方法
http.createServer(app).listen(3000)
//注册路由 login
app.get('login',(req,res)=>{
console.log('login')
res.end("login")
})
/*GET POST*/
const url = require('url')
const changeRes = (res)=>{
res.send = (data)=>{
res.writeHead(200,{"Content-Type":"text/html;charset='utf-8'"})
res.write("<head> <meta charset='UTF-8'></head>")
res.end(data)
}
}
//暴露的模块
const Server = ()=>{
//全局变量
let G = this
//处理get和post请求
G._get = {}
G._post = {}
let app = (req,res)=>{
changeRes(res)
//获取路由
let pathname = url.parse(req.url).pathname
if(pathname.endsWith('/')){
pathname = pathname + '/'
}
//获取请求 -> get/post
let method = req.method.toLowerCase()
if(G['_'+method][pathname]){
if(method === 'post'){
let postStr = ''
req.on('data',(chunk)=>{
postStr += chunk
})
req.on('end',(err,chunk)=>{
if(err){
console.log(err)
return
}
req.body = postStr
console.log('写入数据成功')
})
G['_'+method][pathname](req,res)
}else{
G['_'+method][pathname](req,res)
}
}else{
res.end('no router')
}
}
app.get = (string,callback)=>{
if(!string.endsWith('/')){
string = string + '/'
}
if(!string.startsWith('/')){
string = '/' + string
}
// /login/
G._get[string] = callback
}
app.post = (string,callback)=>{
if(!string.endsWith('/')){
string = string + '/'
}
if(!string.startsWith('/')){
string = '/' + string
}
// /login/
G._post[string] = callback
}
return app
}
module.exports = Server()
/*************使用************/
const http = require('http')
const app = require('./model/router')
const ejs = require('ejs')
http.createServer(app).listen(3000)
app.get('/login',(req,res)=>{
console.log('login')
ejs.renderFile('views/form.ejs',{},(err,data)=>{
res.send(data)
})
//res.send('login')
})
app.post('/dologin',(req,res)=>{
console.log(req.body)
res.send('dologin')
})
END
网友评论