0

I am very new to express and node in general, i have two route handlers with the same path but different method.. I am using express.router() and i was advised to chain the route handlers like this but i get 404 Not found for GET /status but the routes works if i remove my middleware .all(verifytoken)

statusRouter.route('/status')
  .all(verifyToken)
  .get(status.get)
  .post(status.new);

/statusController

 get: 
        (req, res) => {
          Post.find({}, (err, posts) => {
              if(err) throw err;

              if(posts){
                  console.log(posts)
                  res.json({message: 'ok'})
              } 
          })
        },
   new: (req, res) => {
       // i omitted the code for simplicity
   }

/middlewares

    const verifyToken = (req, res, next) => {
   const token = req.headers.authorization.slice(7 - req.headers.authorization.length);
        jwt.verify(token, process.env.KEY1, function(err, decoded) {
            if(err) {
                res.status(401).json({
                    message: "You're session has expired, please login again.",
                    type: 'error',
                    code: 401
                })
            }

            if(decoded){
                next();
            } 
        })
    }
JEEZSUSCRIZE
  • 174
  • 2
  • 16

1 Answers1

0

You need to put it in your express app because it is only a router and has not been integrated to your express application, That's why it is returning 404 because it still cannot find your router.

const express = require('express')
const router = express.Router();
const app = express();

app.use(yourRouter);
bumblebeen
  • 642
  • 8
  • 21
  • I already have that, like i said my routes work without the middleware i only get the 404 if i have it. So the problem is not with the app – JEEZSUSCRIZE Apr 11 '18 at 06:09