当我尝试运行app.js文件时出现此错误。
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a string
这是我的app.js文件
const express = require("express");
const app = express();
app.use("views engine", "ejs");
app.get("/", function(req, res){
res.render("home");
});
app.get("/fellinlovewith/:thing", function(req, res){
var animal = req.param.thing;
res.render("love", {thing:animal});
});
app.get("/posts", function(req, res){
var posts = [
{book1:"author1"},
{book2:"author2"},
{book3:"author3"},
{book4:"author4"}
];
res.render("post", {posts:posts});
});
app.listen(1250,function() {
console.log("server started listining!!!");
});

作为我对这些东西的新手,我不知道什么是路线和中间件。当我用nodemon运行app.js文件时,我得到了同样的错误。
答案 0 :(得分:1)
您需要使用app.set()
,而不是app.use()
,因此请更改:
app.use("views engine", "ejs");
为:
app.set("views engine", "ejs");