2

I am building a crud application using react and node , so i need to check for every request whether the cookie is present or not.

app.all("*", (req,res) => {
 // cookie doesn't exist redirect to login
 if(cookieExist(req.headers.cookie)){
   // how to pass to the next layer ? load the routes below code etc..
  next(); 
 }else{
    res.redirect("/login")
  }
})

const routes = require("./routes/route");

app.use(bodyParser.json());
app.use(cors());

app.use("/apiServices", apiRoutes)

what i am missing here, getting next() as not defined.

Learner
  • 8,379
  • 7
  • 44
  • 82
  • 1
    Possible duplicate of this [How to use the middleware to check the authorization before entering each route in express?](https://stackoverflow.com/questions/18700729/how-to-use-the-middleware-to-check-the-authorization-before-entering-each-route) – Vijay Singh Nov 28 '18 at 03:33
  • but i am getting next() as not defined ? – Learner Nov 28 '18 at 03:35

1 Answers1

2

Define next as argument

app.all("*", (req,res, next) => {
 // cookie doesn't exist redirect to login
 if(cookieExist(req.headers.cookie)){
   // how to pass to the next layer ? load the routes below code etc..
  next(); 
 }else{
    res.redirect("/login")
  }
})
Vijay Singh
  • 132
  • 10