Trong expressj, next() dùng để nhảy tới rout tiếp theo mà match với url khi clien request.
Ví dụ trong đoạn code bên dưới:
- nếu url: /users
- rout * thực hiện visitCounter ++;
- chạy tiếp vào rout /users
- tương tự url: /products
- vào rout * thực hiện visitCounter ++;
- chạy tiếp vào rout /products
app.all('*', function(req, res, next){
visitCounter ++;
next();
});
app.get('/users', function() {
//...
});
app.get('/products', function() {
//...
});