mysql Express路由器错误:未指定默认引擎且未提供扩展

u1ehiz5o  于 2022-12-10  发布在  Mysql
关注(0)|答案(1)|浏览(157)

我正在尝试创建登录页面,当我转到localhost:5000时,我看到以下错误

Error: No default engine was specified and no extension was provided.
at new View (C:\docs\diplom\master3\node_modules\express\lib\view.js:61:11)
at Function.render (C:\docs\diplom\master3\node_modules\express\lib\application.js:587:12)
at ServerResponse.render (C:\docs\diplom\master3\node_modules\express\lib\response.js:1039:7)
at C:\docs\diplom\master3\index.js:9:36
at Layer.handle [as handle_request] (C:\docs\diplom\master3\node_modules\express\lib\router\layer.js:95:5)
at next (C:\docs\diplom\master3\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\docs\diplom\master3\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\docs\diplom\master3\node_modules\express\lib\router\layer.js:95:5)
at C:\docs\diplom\master3\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\docs\diplom\master3\node_modules\express\lib\router\index.js:346:12)

看起来错误在这里app.set("view_engine", "ejs"); app.set("views", "./views");,但我不明白哪里出了问题。例如,这是index.js

const express = require("express");
const db = require("./routes/db-config");
const app = express();
const cookie = require("cookie-parser");
const PORT = process.env.PORT || 5000;
app.use("./js", express.static(__dirname + "./public/js"));
app.use("./css", express.static(__dirname + "./public/css"));
app.set("view_engine", "ejs");
app.set("views", "./views");
app.use(cookie());
app.use(express.json());
db.connect((err)=> {
    if(err) throw err;
});
app.use("/", require("./routes/pages"));
app.use("/api", require("./controllers/auth"));
app.listen(PORT);

Pages.js

const express = require("express");
const router = express.Router();
router.get("/", (req, res) => {res.render("index");});
router.get("/register", (req, res) => {res.sendFile("register.html", {root: "./public"})});
router.get("/login", (req, res) => {res.sendFile("login.html", {root: "./public/"})});
module.exports = router;

Db_config.js

const sql = require("mysql");
const db = sql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'Moto01)!',
    database: 'master'
});
module.exports = db;

Auth.js

const express = require("express");
const register = require("./register");
const login = require("./login");
const logout =require("./logout");
const router = express.Router();

//router.post("/register", register);
//router.post("/login", login);
//router.post("/logout", logout);

module.exports = router;

Index.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>Home Page</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css" 
    integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    </head>
    <body>
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <div class="container-fluid">
                <a class="navbar-brand" href="#">Home page</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
            aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link active" aria-current="page" href="#">Home</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/register">Register</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/login">Login</a>
                </li>
            </ul>
        </div>
        </div>
    </nav>
    <div class="container my-5">
        <div class="card" style="width: 100%">
            <div class="card-body">
                <h5 class="cardt-itle">Welcome</h5>
                <h6 class="card-subtitle mb-2 text muted">Card subtitle</h6>
                <p class="card-text"></p>
                <button class="btn btn-primary"><a href="/login">Login</a></button>
                <button class="btn btn-primary"><a href="/register">Register</a></button>
            </div>
        </div>
    </div>
</body>

我试着调查这个错误的其他案例,但没有一个对我起作用。这里出了什么问题?

q8l4jmvw

q8l4jmvw1#

您在index.js文件中有一个错误,第八行

app.set("view_engine", "ejs");
             ^

它应该是这样的:

app.set('view engine', 'ejs');

参考自expressejs网站,官方文件。

相关问题