I have a Heroku app using Node.js and Express, and I want to determine whether or not a certain file exists. However, it seems like the file is never found. I have the following path:
JavaScript
x
10
10
1
app.get('/sims/:sim_name', (req, res) => {
2
var simName = req.params.sim_name; // get the name of the requested sim from the URL
3
// if it exists, render it
4
if (fs.existsSync(`pages/sims/${simName}.ejs`))
5
res.render(`pages/sims/${simName}`);
6
// otherwise, show an error message
7
else
8
res.send("Sorry, the page you requested does not exist.");
9
});
10
The directory structure of my project looks like this:
JavaScript
1
29
29
1
.
2
├── Procfile
3
├── README.md
4
├── app.json
5
├── index.js
6
├── node_modules
7
│ ├── so many node_modues
8
├── package-lock.json
9
├── package.json
10
├── public
11
│ ├── images
12
│ │ └──
13
│ ├── lang-logo.png
14
│ ├── node.svg
15
│ ├── scripts
16
│ │ └──
17
│ ├── stylesheets
18
│ │ ├──
19
│ └── test.html
20
├── test.js
21
└── views
22
├── pages
23
│ ├── index.ejs
24
│ ├── sims
25
│ │ └── EField.ejs
26
└── partials
27
├── header.ejs
28
└── nav.ejs
29
I am doing something wrong here? Or is this a Heroku issue?
Advertisement
Answer
Ok, I needed to change pages/sims/${simName}.ejs
to views/pages/sims/${simName}.ejs
.
I had previously tried /views/pages/sims/${simName}.ejs
, which didn’t work because of how the file paths work in Heroku.
I also added braces, as per the suggestion of @UserToday (although this wasn’t necessary).