Search code examples
javascripthtmlnode.jsnodemon

I get error in node js in app.js file in app.set


I don't know why I get an error but if you help i will be very thankful.

Here's My CODE of the app.js and important note I have downloaded express and body-parser through the node package manager.

const express = require('express');
const bodyParser = require('body-parser');

const app = express;

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

app.get("/",function(req,res){
    var today = new Date();
    var option = {
        weekday:"long",
        day:"numeric",
        month:"long"
    };
    var day = today.toLocaleDateString("en-US",option);
res.sendfile(__dirname + "/index.html");
res.render('list', {kindOfDay:day});

});
app.listen(3000,function(){
  console.log("Server started on port 3000");
});

My Error

C:\Users\a\Desktop\Todo-List v1\app.js:6
app.set('view engine', 'ejs');
    ^
TypeError: app.set is not a function
    at Object.<anonymous> (C:\Users\a\Desktop\Todo-List v1\app.js:6:5)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...

Solution

  •   Loading express module returns a function. So, Please try changing your code like this.
      const app = express();
    
    const express = require('express');
    const bodyParser = require('body-parser');
    
    const app = express();
    
    app.set('view engine', 'ejs');
    
    app.get("/",function(req,res){
        var today = new Date();
        var option = {
            weekday:"long",
            day:"numeric",
            month:"long"
        };
        var day = today.toLocaleDateString("en-US",option);
    res.sendfile(__dirname + "/index.html");
    res.render('list', {kindOfDay:day});
    
    });
    app.listen(3000,function(){
      console.log("Server started on port 3000");
    });