Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

node express API use #1

Open
wwELi opened this issue Aug 16, 2017 · 0 comments
Open

node express API use #1

wwELi opened this issue Aug 16, 2017 · 0 comments

Comments

@wwELi
Copy link
Owner

wwELi commented Aug 16, 2017

var express = require('express')
var app = express();
app.use(function(req,res,next){
// do something
});

//等同以下代码
var http = require('http');
function express(){
var funcs = [];

var expr = function(req,res){
    var i = 0;
    function next(){            
        var task = funcs[i++];
        if(!task) return;
        task(req,res,next);
    }
    next();
}
expr.use=function(f){
    funcs.push(f);
}
return expr;

}
var app = express();

app.use(function(req,res,next){
console.log('haha');
next();
});
app.use(function(req,res,next){
console.log('hehe');
next();
});
app.use(function(req,res){
res.end("there is nothing happened");
});

http.createServer(app).listen('3000', function(){
console.log('Express server listening on port 3000');
});

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant