1
node server
を実行すると、エラーが発生し、根本原因を突き止めることができません。私は以下のエラーを貼り付けています。私はスタックオーバーフローポストを通過し、問題をデバッグしようと多くの時間を費やしてきたので、本当に助けていただければ幸いです。ありがとうございました。不正ルートのエラーを取得する
ERROR:
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);
});
});
コードを投稿しない場合はお手伝いできません。 –
ありがとう!私はちょうどそれを追加した。 – Monica
'use'に入る関数の1つが、' app.use( "/"、apiRoutes);のように未定義です。 'apiRoutes'はここで定義されていますか? – Jorg