NodeJS “视图引擎”,“ejs”);类型错误(^T):app.set不是函数

9cbw7uwe  于 2022-12-03  发布在  Node.js
关注(0)|答案(1)|浏览(160)

express验证中的模板引擎出现问题
我希望能解决我的问题

应用程序.js

const express=require('express')
const app= express;

const port = 5000;

//set template engine
app.set('view engine','ejs')

//navigation
app.get('',(req,res)=>{
    res.render('index') 
})

app.get('/resgiter',(req,res)=>{
    res.render('resgiter')
})

app.listen(port,() =>
    console.info(`App listening on port:${port}`)
    )

模板引擎出错

vlju58qv

vlju58qv1#

错过在代码的第二行调用express
代替:

const app = express;

您应提供:

const app = express();

正斜杠/

app.get("/", (req, res) => {
  res.render("index");
});
    • 应用程序. js**
const express = require("express");
const app = express();

const port = 5000;

//set template engine
app.set("view engine", "ejs");

//navigation
app.get("/", (req, res) => {
  res.render("index");
});

app.get("/resgiter", (req, res) => {
  res.render("resgiter");
});

app.listen(port, () => console.info(`App listening on port:${port}`));
    • 索引. ejs**
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>ejs it's Awesome! ;-)</h1>
  </body>
</html>
    • 文件夹和文件结构和控制台输出**:

    • 浏览器输出**:

相关问题