Reactjs 将中间件代码从app.js移动到it';自己的函数和文件

Reactjs 将中间件代码从app.js移动到it';自己的函数和文件,reactjs,jwt,middleware,next.js,Reactjs,Jwt,Middleware,Next.js,在我的app.js文件中,我有一些用于身份验证的中间件,如下所示: const jwt = require("jsonwebtoken"); const jwtSecret = "mysuperdupersecret"; // actually in .env file app.use((req, res, next) => { if (req.path == "/api/login") { return next(); } const token = req.he

在我的app.js文件中,我有一些用于身份验证的中间件,如下所示:

const jwt = require("jsonwebtoken");
const jwtSecret = "mysuperdupersecret"; // actually in .env file

app.use((req, res, next) => {
  if (req.path == "/api/login") {
    return next();
  }

  const token = req.headers.authorization;


  try {
    var decoded = jwt.verify(token, jwtSecret);
    console.log("decoded", decoded);
  } catch (err) {
    // Catch the JWT Expired or Invalid errors
    return res.status(401).json({ msg: err.message });
  }

  next();
});
我已经创建了一个中间件文件夹,并在其中放置了一个名为“auth.js”的文件。然后将此代码放入其中:

const jwt = require("jsonwebtoken");
const jwtSecret = "mysuperdupersecret";

function auth(req, res, next) {
  if (req.path == "/api/login") {
    return next();
  }

  const token = req.headers.authorization;


  try {
    var decoded = jwt.verify(token, jwtSecret);
    console.log("decoded", decoded);
    next();
  } catch (err) {
    return res.status(401).json({ msg: err.message });
  }
}

module.exports = auth;
在我这么做之前,我的申请是有效的。现在我得到一个500内部服务器错误;消息“:“未定义jwt”

app.js

const express = require("express");
const app = express();

// CORS middleware
app.use(function(req, res, next) {
  // Allow Origins
  res.header("Access-Control-Allow-Origin", "*");
  // Allow Methods
  res.header(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, PUT, DELETE, OPTIONS"
  );
  // Allow Headers
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, Accept, Content-Type, Authorization"
  );
  // Handle preflight, it must return 200
  if (req.method === "OPTIONS") {
    // Stop the middleware chain
    return res.status(200).end();
  }
  // Next middleware
  next();
});

// Routes
app.get("/api/login", (req, res) => {
  const token = jwt.sign({ username: "test" }, jwtSecret, { expiresIn: 60 }); // 1 min token
  res.json({ token: token });
});

app.get("/api/token/ping", (req, res) => {
  res.json({ msg: "all good mate" });
});

app.get("/api/ping", (req, res) => {
  res.json({ msg: "pong" });
});

module.exports = app;

如何使其重新工作?

问题在于
/app/login
路径中,您正在使用
jwt
模块,而没有将其导入
app.js
中,而且
jwtSecret
现在也不在app.js中

我也看不到你是如何使用你在应用程序中创建的
auth.js

您可以将auth.js更新为

const jwt = require("jsonwebtoken");
const jwtSecret = "mysuperdupersecret";

function auth(req, res, next) {
  if (req.path == "/api/login") { // check for HTTP GET/POST if required
    const token = jwt.sign({ username: "test" }, jwtSecret, { expiresIn: 60 }); // 1 min token
    res.json({ token: token });
  }
  else {
    const token = req.headers.authorization;
    try {
      var decoded = jwt.verify(token, jwtSecret);
      console.log("decoded", decoded);
      next();
    } catch (err) {
      return res.status(401).json({ msg: err.message });
    }
  }
}
在app.js中

/*.
.
. other code
*/
const auth = require('./auth.js');

app.use(auth);

/*.
. remaining code
.
*/

显示更新后的app.js too你仍然在
app.js
中引用
jwt
,但你已经删除了导入。而且你实际上似乎没有在
app.js
中使用
auth.js
,但我在auth.js中导入了它们。我是要在app.js中包含auth.js吗?但我要在auth.js中导入它们。我是要在auth.js中包含auth.js吗s在app.js中?就app.js而言,你没有从auth.js导入任何东西,你的应用程序对它的存在一无所知:)谢谢。我更新了代码,但仍然收到500服务器错误?相同的错误?你能用新代码更新这个问题吗?你正在导入auth.js,但没有导出它。所以它的变量作用域是有限的。据说它有模块作用域。除非它是从模块导出并导入到另一个需要它的模块中,否则不能从模块外部访问它,