letmid=(req,res,next)=>{req.date=newDate();// Attach date on req objectconsole.log("mid",req);// res.redirect("/books/1"); // Redirct to a route// next("no auth"); //Throw Errornext();// Call the next middleware};letauth=(req,res,next)=>{console.log("auth required");next();};letrouter=Navamid("/"(uri,req,res)=>console.log('404'),(err,req,res)=>console.log(err));// Attach routesrouter.use(mid).on("/",(req,res,next)=>{console.log("~> /",req.date);}).on("/users/:username",auth,({ params })=>{console.log("~> /users/:username",params);}).on("/books/*",auth,(req,res)=>{console.log("~> /books/*",req);});router.listen()
Difference from navaid
1. constructor navamid(base, on404, onError)
- Optional on404 and onError when there is any middleware errors.
- on404(uri, req, res)
- onError(err, req, res)
2. use(middleware(s))
- An array of global middlewares similar to express.js
- (req, res, next) - Middlware signature
3. on(path, middleware(s))
- An array of middlewares to be executed for the path