1

I'm taking a Linkedin course on building a website using express.js I'm trying to get my index.ejs page to render, however the server keeps rendering my index.html page.

I've already checked other overflow articles but it just shows to switch params (req,res), put ("index"), and to combine the app.get statement.

Here is my server.js code

const { response } = require('express');
const express = require('express');
const path = require('path');

const app = express();
const port = 3000;

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

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

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

app.get('/', (req, res) => {
  response.render('index', { pageTitle: 'Welcome' });
  });

app.get('/speakers', (req, res) => {
  response.sendFile(path.join(__dirname, './static/speakers.html'));
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

If I remove my index.html file I get this error message 👇🏾

TypeError: Cannot read properties of undefined (reading 'app')
    at ServerResponse.render (D:\building-website-nodejs-express\node_modules\express\lib\response.js:1017:22)
    at D:\building-website-nodejs-express\server.js:24:12
    at Layer.handle [as handle_request] (D:\building-website-nodejs-express\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\building-website-nodejs-express\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (D:\building-website-nodejs-express\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (D:\building-website-nodejs-express\node_modules\express\lib\router\layer.js:95:5)
    at D:\building-website-nodejs-express\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (D:\building-website-nodejs-express\node_modules\express\lib\router\index.js:346:12)
    at next (D:\building-website-nodejs-express\node_modules\express\lib\router\index.js:280:10)
    at SendStream.error (D:\building-website-nodejs-express\node_modules\serve-static\index.js:121:7)

Here is my folder structure 👉🏾 Picture Of VS Code Folders and Server.js

THIS IS THE CODE THAT RESOLVED MY ISSUE 👇🏾

const express = require('express');
const path = require('path');

const app = express();

const port = 3000;

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

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

app.get('/', (request, response) => {
  response.render('index', { pageTitle: 'Welcome' });
});

app.get('/speakers', (request, response) => {
  response.sendFile(path.join(__dirname, './static/speakers.html'));
});

app.listen(port, () => {
  console.log(`Express server listening on port ${port}!`);
});

1
  • Shouldn't it be res.render(...) instead of response.render(...) as you want to use the response object for the current request? Commented Aug 23, 2022 at 15:50

2 Answers 2

2

Please make sure your filepath is correct, normally it should like this.

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

    app.use(express.static(path.join(__dirname, 'static')));

Please make sure your index.ejs is in the views folder. Change the "response" to "res" since your parameter in function it "res".

    app.get('/', (req, res) => {
      res.render('index', { pageTitle: 'Welcome' });
    });
2
  • I added these changes and confirming the folder structure is correct. thanks for answer.
    – Don Lamarr
    Commented Aug 23, 2022 at 16:39
  • This is resolved by... deleting index.html file, moving index.ejs out of "views/page" to "views" folder. changed the res.render('page/index') to res.render('index'). Refreshed my code and now ejs is working properly.
    – Don Lamarr
    Commented Aug 24, 2022 at 0:49
1

This applies for all the times you used response. change it to res.

1
  • Your answer is good, but for clarity, you should make it a bit more detail, like Titan XP's answer. Paste the incorrect code, explain what's wrong, then paste the fixed code. Commented Aug 27, 2022 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.