1) Middleware를 만들어서 사용하는 방법
var app = express.createServer();
// Your own super cool function
var logger = function(req, res, next) {
console.log("GOT REQUEST !");
next(); // Passing the request to the next handler in the stack.
}
app.configure(function(){
app.use(logger); // Here you add your logger to the stack.
app.use(app.router); // The Express routes handler.
});
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
2) app.all() 함수를 사용하는 방법
...
app.all('/*', function(req, res){
console.log('GOT REQUEST !');
});
...
이상의 두가지 방법이 있으나 2)의 방법은 해당 함수를 호출하는 위치에 따라 다른 GET, POST 리퀘스트가 처리되지 않을 수 도 있으니 항상 다른 리퀘스트보다 늦게 등록이 되어야 한다.
No comments:
Post a Comment