来自 app.js
// REQUIRE ROUTES
var commentRoutes = require("./routes/comments"),
bpostRoutes = require("./routes/bposts"),
indexRoutes = require("./routes/index");
//USE ROUTES
app.use("/", indexRoutes);
app.use("/bposts", bpostRoutes);
app.use("/bposts/:id/comments", commentRoutes);
来自路由/index.js
var express = require("express");
var router = express.Router();
//LANDING -root route
router.get("/", function(req, res){
res.render("landing");
});
module.exports = router;
我目前正在尝试在创建数据库或为博客做任何有意义的事情之前设置我的所有路线。第一次遇到这个问题是因为没用
module.exports = router;
在我拥有的每个路线页面上。Express 路由器已安装并保存到 package.json。每次我遇到这个错误时,它都是一个快速修复,因为我没有包含导出语句。现在我终于记得添加它了,我仍然收到此错误消息。任何建议或意见,将不胜感激!
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a Object
at Function.use (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:458:13)
at EventEmitter.<anonymous> (/home/ubuntu/workspace/node_modules/express/lib/application.js:220:21)
at Array.forEach (native)
at EventEmitter.use (/home/ubuntu/workspace/node_modules/express/lib/application.js:217:7)
at Object.<anonymous> (/home/ubuntu/workspace/app.js:25:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3