2

i am new to backend web development and i was practicing on a project in nodejs and express.js Below is the code of my app.js file

const express = require("express")
const bp = require("body-parser")
const app = express()
app.set("view-engine", "ejs")

app.get("/", (req, res) => {
const date = new Date()
const curDay = date.getDay()
let day = ""
if (curDay === 6 || curDay === 0){
  day = "weekend"
} else{
  day = "weekday"
}
  res.render("list", {kindOfDay: day})
})

app.listen(3000, () => {
  console.log("Listening on port 3000")
})

And below is the code of my list.ejs file

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="UTF-8">
    <title>To-Do List App</title>
  </head>
  <body>
    <h1>It's a <%= kindOfDay %>!</h1>
  </body>
</html>

When i try to run the app using nodemon app.js command it throws me an error back which says

Error: No default engine was specified and no extension was provided. at new View (/storage/emulated/0/todo-list-app-ver_1/node_modules/express/lib/view.js:61:11) at Function.render (/storage/emulated/0/todo-list-app-ver_1/node_modules/express/lib/application.js:570:12) at ServerResponse.render (/storage/emulated/0/todo-list-app-ver_1/node_modules/express/lib/response.js:1012:7) at /storage/emulated/0/todo-list-app-ver_1/app.js:15:7 at Layer.handle [as handle_request] (/storage/emulated/0/todo-list-app-ver_1/node_modules/express/lib/router/layer.js:95:5) at next (/storage/emulated/0/todo-list-app-ver_1/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/storage/emulated/0/todo-list-app-ver_1/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/storage/emulated/0/todo-list-app-ver_1/node_modules/express/lib/router/layer.js:95:5) at /storage/emulated/0/todo-list-app-ver_1/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/storage/emulated/0/todo-list-app-ver_1/node_modules/express/lib/router/index.js:335:12)

Expected output should be

it's a weekday

or

it's a weekend

i don't know what's causing the error. My node version is 16.13.0 and my express version is 4.17.1

metodribic
  • 1,561
  • 17
  • 27
Nasyx Nadeem
  • 241
  • 2
  • 12

1 Answers1

1

You have typo in app.set("view-engine", "ejs") instead should be app.set("view engine", "ejs") and it Works like a charm ;-)

const express = require("express");

const app = express();
app.set("view engine", "ejs");

app.get("/", (req, res) => {
  const date = new Date();
  const curDay = date.getDay();
  let day = "";
  if (curDay === 6 || curDay === 0) {
    day = "weekend";
  } else {
    day = "weekday";
  }
  res.render("list", { kindOfDay: day });
});

app.listen(3000, () => {
  console.log("Listening on port 3000");
})
MarioG8
  • 5,122
  • 4
  • 13
  • 29