When i tried to run node app.js from my server. It brought the error below:
C:UsersBLUE HOSTDesktoptodolist-v1>node app.js
C:UsersBLUE HOSTDesktoptodolist-v1node_modulesexpresslibrouterindex.js:464
throw new TypeError(‘Router.use() requires a middleware function but got a ‘ + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a string
at Function.use (C:UsersBLUE HOSTDesktoptodolist-v1node_modulesexpresslibrouterindex.js:464:13)
at Function. (C:UsersBLUE HOSTDesktoptodolist-v1node_modulesexpresslibapplication.js:220:21)
at Array.forEach ()
at Function.use (C:UsersBLUE HOSTDesktoptodolist-v1node_modulesexpresslibapplication.js:217:7)
at Object. (C:UsersBLUE HOSTDesktoptodolist-v1app.js:6:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
This is my app.js file:
const express = require("express");
const app = express();
app.use(express.urlencoded({extended: true}));
app.use("view engine", "ejs");
app.get("/",function(req,res) {
var today = new Date();
var currentDay = today.getDay();
var day = "";
if (currentDay === 6 || currentDay === 0) {
day = "weekend";
} else{
day = "weekday";
}
res.render("list", { kindOfDay: day });
});
app.listen(3000, function() {
console.log("Listening on port 3000");
});
This is my list.ejs file
<!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>To do List</title>
</head>
<body>
<h1><%= kindOfDay %></h1>
</body>