Home > Enterprise >  I found error in ejs. Please tell me where it goes wrong?
I found error in ejs. Please tell me where it goes wrong?

Time:10-20

I don't understand where my code goes wrong. Please help me. Whenever I run this code (nodemon app.js in terminal) I got error(error:- can't get) in the browser. Thank you in advance for helping me.

app.js code:- 

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

app.set("/",function (req,res) {
    var today=new Date();
    var day="";
    if(today.getDay()==6||today.getDay()==0)
    day="weekend";
    else
    day="weekday";
    res.render("todo", { kindofDay: day});
});

app.listen(3000,function() {
    console.log('server is starting...');

});

todo.ejs code:-

<!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>Ejs day list</title>
</head>
<body>
    <h1>It's a <%= kindofDay %> !</h1>
</body>
</html>

CodePudding user response:

You have to use app.get

app.get("/",function (req,res) {
    var today=new Date();
    var day="";
    if(today.getDay()==6||today.getDay()==0)
    day="weekend";
    else
    day="weekday";
    res.render("todo", { kindofDay: day});
});
  • Related