gpt4 book ai didi

javascript - 在不正确的路线上收到错误消息

转载 作者:太空宇宙 更新时间:2023-11-03 22:10:06 25 4
gpt4 key购买 nike

当我运行 Node 服务器时,我收到错误并且无法找出根本原因。我将错误粘贴到下面。我真的很感激一些帮助,因为我花了很多时间浏览堆栈溢出帖子并尝试调试问题。谢谢。

错误:

C:\Users\desktop\studentSearch\node_modules\express\lib\router\index.js:458
throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
^

TypeError: Router.use() requires middleware function but got a undefined
at Function.use (C:\Users\Monica\desktop\studentSearch\node_modules\express\lib\router\index.js:458:13)
at Function.<anonymous> (C:\Users\Monica\desktop\studentSearch\node_modules\express\lib\application.js:220:21)
at Array.forEach (native)
at Function.use (C:\Users\Monica\desktop\studentSearch\node_modules\express\lib\application.js:217:7)
at Object.<anonymous> (C:\Users\Monica\desktop\studentSearch\server.js:46:5)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3

代码

// ******************************************************************************
// *** Dependencies
// =============================================================
var express = require("express");
var exphbs = require("express-handlebars");
var bodyParser = require("body-parser");
var methodOverride = require("method-override");
jwtExp = require("express-jwt");
tokenSecret = require("./tokensecret.js");
cookieParser = require("cookie-parser");
// Sets up the Express App
// =============================================================
var app = express();
var PORT = process.env.PORT || 8080;

//cookie parser
app.use(cookieParser(tokenSecret));

// Set Handlebars as the default templating engine.
app.engine("handlebars", exphbs({ defaultLayout: "main" }));
app.set("view engine", "handlebars");

// Requiring our models for syncing
var db = require("./models");

// Sets up the Express app to handle data parsing
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text());
app.use(bodyParser.json({ type: "application/vnd.api+json" }));

// Override with POST having ?_method=DELETE
app.use(methodOverride("_method"));

// Routes =============================================================

require("./routes/html-routes.js")(app);
var apiRoutes = require("./routes/api-routes.js")(app);
var authRoutes = require("./routes/auth-routes.js");

// API MIDDLEWARE
// app.use("/api", jwtExp({ secret: tokenSecret }));
app.use("/api", apiRoutes);


// AUTH MIDDLEWARE
app.use("/auth", authRoutes);
app.use("/auth", express.static("./public"));

// USER MIDDLEWARE
app.get("/", jwtExp({
secret: tokenSecret,
getToken: function fromCookie(req) {
if (req.signedCookies) {
return req.signedCookies.jwtAuthToken;
}
return null;
},
credentialsRequired: false
}), function(req, res, next) {
if (req.user) {
next();
} else {
res.redirect("/auth/login");
}
});

app.use("/", apiRoutes);

app.use(express.static("./public"));



// Syncing our sequelize models and then starting our express app
db.sequelize.sync({}).then(function() {
app.listen(PORT, function() {
console.log("App listening on PORT " + PORT);
});
});

最佳答案

问题在于定义中包含 (app)。见下文。

var apiRoutes = require("./routes/api-routes.js")(app); 

关于javascript - 在不正确的路线上收到错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44960075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com