jsconst woa=require('./woa');
let fs=require('fs')
let app=new woa();
app.use(async (ctx,next)=>{
let p=new Promise((resolve)=>{
setTimeout(()=>{
resolve();
},2000)
});
await p.then(()=>{
console.log(1);
})
next();
console.log('end',1);
})
app.use((ctx,next)=>{
console.log(2);
console.log('end',2);
})
app.use((ctx,next)=>{
console.log(3);
})
app.listen(9092,()=>{
console.log('http://localhost:9092')
});
jsconst http=require('http');
let context= {
get url(){
return this.req.url
},
set url(val){
this.req.url=val;
},
get body(){
return this.res.body;
},
set body(val){
this.res.body=val;
}
};
class Application {
constructor(){
this.callbacks=[]
}
use(callback){
this.callbacks.push(callback||function(){});
}
middleware(ctx,index){
if(index==null)index=-1;
index++;
let callback=this.callbacks[index];
if(!callback)return;
callback(ctx,()=>{
this.middleware(ctx,index);
})
}
listen(...args){
const server=http.createServer(async (req,res)=>{
let ctx=this.createCtx(req,res);
if(this.callbacks.length>0){
this.middleware(ctx);
}
// await this.callback(ctx);
res.end(ctx.body)
});
server.listen(...args);
}
createCtx(req,res){
let ctx=Object.create(context);
ctx.req=req;
ctx.res=res;
ctx.res.body='';
return ctx;
}
}
module.exports=Application;
本文作者:BARM
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!