当我运行node server
时,我收到错误,无法找出根本原因。我正在粘贴下面的错误。我真的很感激一些帮助,因为我花了很多时间浏览堆栈溢出帖子并尝试调试问题。谢谢。
错误:
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
CODE
// ******************************************************************************
// *** 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);
});
});
答案 0 :(得分:0)
问题在于定义中的(app)。见下文。
var apiRoutes = require("./routes/api-routes.js")(app);