嗨,我在启动app.js时遇到“ app.use()要求中间件功能错误”错误。
我想做的是使用okta auth作为中间件功能来验证访问我的Web应用程序的人。我将身份验证部分作为单独的js文件以及在调用“ api / ...”之前要使用的内容 这是appjs:
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var oktaAuth = require("./api/auth/auth.js")
var routes = require("./api/routes");
var http = require("http");
var morgan = require('morgan');
require("./api/helper/global_function");
// Defining the port and env to run on
const port = 8000;
var app = express();
app.set("port", port);
// To Allow CORS calls
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', true);
return next();
});
app.use(morgan('combined'));
// Enable parsing of posted forms
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Set static directory before defining routes
app.use(express.static(path.join(__dirname, "dist")));
app.use("/node_modules", express.static(__dirname + "/node_modules"));
app.use(oktaAuth)
app.use("/api", routes);
app.use("*", (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
// To verify the listen is up and running
var server = app.listen(app.get("port"), function () {
var port = server.address().port;
console.log("Magic happens on port " + port);
});
,并在此行“ app.use(oktaAuth)”中引发错误。 这是api / auth / auth.js的代码
const OktaJwtVerifier = require('@okta/jwt-verifier');
const oktaJwtVerifier = new OktaJwtVerifier({
clientId: 'xxxx',
issuer: 'xxxx'
});
module.exports.oktaAuth = async function (req, res, next) {
try {
const token = req.token;
if (!token) {
return res.status(401).send('Not Authorised');
}
const jwt = await oktaJwtVerifier.verifyAccessToken(token);
console.log(jwt);
next();
}
catch (err) {
return res.status(401).send(err.message);
}
}
对错误有任何建议吗?