4

TypeError: res.status is not a function at auth (D:\PROJECT\Web Application\Learning React\MERN STACK\middleware\auth.js:17:9). I am getting this error. The code is given below.

const config = require("config");
const jwt = require("jsonwebtoken");

function auth(res, req, next) {
  const token = req.header("x-auth-token");

  // Check for token
  if (!token) res.status(401).json({ msg: "No token, authorization denied" });

  try {
    // verify token
    const decoded = jwt.verify(token, config.get("jwtSecret"));
    // Add user from payload
    req.user = decoded;
    next();
  } catch (e) {
    res.status(400).json({ msg: "Token is not valid" });
  }
}

module.exports = auth;

Error ScreenShot

Waleed ur Rehman
  • 109
  • 2
  • 11

1 Answers1

5

It should be req, res, next

Change

function auth(res, req, next) {

to

function auth(req, res, next) {

https://expressjs.com/en/guide/using-middleware.html

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107