Home > database >  Express router Error: No default engine was specified and no extension was provided
Express router Error: No default engine was specified and no extension was provided

Time:12-08

I'm trying to create a login page and when i go localhost:5000 i see this error

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)

it looks like the error is here app.set("view_engine", "ejs"); app.set("views", "./views"); but i don't get what is wrong. for instance this is 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/[email protected]/dist/css/bootstrap.min.css" 
    integrity="sha384-HSMxcRTRxnN Bdg0JdbxYKrThecOKuH5zCYotlSAcp1 c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    </head>
    <body>
        <nav >
            <div >
                <a  href="#">Home page</a>
                <button  type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
            aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span ></span>
        </button>
        <div  id="navbarNav">
            <ul >
                <li >
                    <a  aria-current="page" href="#">Home</a>
                </li>
                <li >
                    <a  href="/register">Register</a>
                </li>
                <li >
                    <a  href="/login">Login</a>
                </li>
            </ul>
        </div>
        </div>
    </nav>
    <div >
        <div  style="width: 100%">
            <div >
                <h5 >Welcome</h5>
                <h6 >Card subtitle</h6>
                <p ></p>
                <button ><a href="/login">Login</a></button>
                <button ><a href="/register">Register</a></button>
            </div>
        </div>
    </div>
</body>

I tried looking in to other cases of this error but none of them worked for me. What is the problem here?

CodePudding user response:

You have a bug in index.js file, line number eight:

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

It's should looks like that :

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

Refrence from express and ejs website, official docs.

  • Related