NodeJS - Trying to render index.ejs file, but still only servers index.html that is in my public folder

Viewed 975

The code below still serves the index.html page that's in the public folder, but I want to render index.ejs. I followed the video example so I'm not sure why he's able to render the ejs file whereas I'm still only seeing the index.html file.

The video example I am using seems to be the same. My package.json file has "main": "app.js".

Here is the video example link, and code below: https://youtu.be/gnsO8-xJ8rs?t=28m12s

//View Engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

//Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : false}));

//Set Static path
app.use(express.static(path.join(__dirname, 'public')));

//Routes
app.get('/', (req, res) => {//homepage represented by the single slash
  res.render('index');
});

app.listen(3000, () => {
  console.log('server started on port 3000...');
});

I noticed that if I comment out

//Routes
app.get('/', (req, res) => {//homepage represented by the single slash
  res.render('index');
});

then the ejs file is rendered and served.

Any advice would be appreciated!

1 Answers

Note that EJS by default looks into 'Views' folder

app.set('views', path.join(__dirname, 'views'));

You can specify the file path to look in your folder as Views change this to your public folder and It will be up and running.

Related