Reputation: 11809
I have the following code in my express app (app.js file):
const express = require("express");
const app = express();
app.set('view engine', 'ejs');
app.get('/', function(req, res) {
res.render('index');
});
app.listen(8080);
And the following dependencies:
"ejs": "^2.6.1",
"express": "^4.16.3",
"request": "^2.87.0"
And this is my folder structure:
myapp
app.js
package.json
views
index.ejs
node_modules
[all node files]
But when I run the app, it shows this error:
Failed to lookup view "index.ejs" in views directory "mylocaldirectory/myapp/views"
If it would help here are the other error messages:
at Function.render (/mylocaldirectory/myapp/node_modules/express/lib/application.js:580:17)
at ServerResponse.render (/mylocaldirectory/myapp/node_modules/express/lib/response.js:1008:7)
at //mylocaldirectory/myapp/app.js:11:9
at Layer.handle [as handle_request] (/mylocaldirectory/myapp/node_modules/express/lib/router/layer.js:95:5)
at next (/mylocaldirectory/myapp/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/mylocaldirectory/myapp/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/mylocaldirectory/myapp/node_modules/express/lib/router/layer.js:95:5)
at /mylocaldirectory/myapp/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/mylocaldirectory/myapp/node_modules/express/lib/router/index.js:335:12)
at next (/mylocaldirectory/myapp/node_modules/express/lib/router/index.js:275:10)
I get no error if I just use res.send("SOME TEXT")
but when I try to render an ejs file, it doesn't. What's the issue?
Upvotes: 11
Views: 28229
Reputation: 1
I also got the same error. By doing below steps, I got the Result.
Upvotes: 0
Reputation: 25
The express guide on template engines talks about setting up the 'views' and the 'view engine'. I also found it important / interesting to talk about redundancy. All detailed below.
By default, it looks for the template files (be them .ejs, .pug, etc.) on a subfolder called "views".
So you can either set up this subfolder called "views" with your .ejs files in it or set this 'views' folder to any that you wish.
Using app.set('views', './')
, for example, sets the 'views' folder to be the current folder (i.e. no subfolders needed).
The guide also points out that you can either specify the engine (ejs, pug, etc.) or specify the file extension when using, for example, res.render(<filename>)
.
So, a very redundant way of doing things is (before any routing)
app.set('views', './views');
app.set('view engine', 'ejs');
app.get("/", function(req,res) {
res.render("index.ejs");
});
with a folder structure like
myapp
app.js
package.json
views
index.ejs
node_modules
[all node files]
Since you have a folder called "views" with your .ejs files in the same level of your app.js file, you can omit the app.set('views', './views')
command.
Since you call res.render
with the file extension, you can omit the app.set('view engine', 'ejs')
command.
Upvotes: 2
Reputation: 1
Make Sure you have corrected file name with ejs extension. Make Sure you have corrected folder name views. app.set('view engine', 'ejs')
It will work properly
Upvotes: -1
Reputation: 13
In your case, 'app.set' function is unable to find 'views' folder.
So use following...
app.set('views', __dirname + '/views')
app.set('view engine', 'ejs')
or you can do as following
const path = require('path')
//path is inbuilt module in nodejs, so don't need to install it.
app.set('views', path.join(__dirname, '/views'))
app.set('view engine', 'ejs')
both methods are same.
__dirname will concatenate absolute path to 'views' folder.
(note: Notice that . is should not be there while concatenating with absolute directory path. So don't use './views' after __dirname, use '/views' only)
Upvotes: 1
Reputation: 49
app.set('views', './View'); //./View(You already created folder)
app.set('view engine', 'ejs');
res.render('index', { kindDay: day });
index is my EJS file. kindDay is EJS file-created key.
Upvotes: -3
Reputation: 89
I used this:
const express = require("express");
const server = express();
const path = require("path");
server.set("views", path.join(__dirname, "views"));
server.set("view engine", "ejs");
server.use(express.static("public"));
server.get("/", (req, res) => {
res.render("index");
});
server.listen(8080);
Upvotes: 4
Reputation: 69
try this it worked for me.
// view engine setup
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
Upvotes: 0
Reputation: 121
This issue basically comes when your ejs views engine unable to find file or folder. So do following checks:
1.check directory name. it should be 'views' not 'view'.
2.check file under directory having correct file extension or not? it should be .ejs
only not others like .html
or .json
etc.
3.tell express that i am going to use ejs view engine by using below code:
app.set('view engine','ejs');
Upvotes: 4
Reputation: 756
make sure you have only css files in public folder .other folders can be outside the public folder.
Upvotes: 0
Reputation: 6898
First you have to tell express what view engine to use by setting
javascript
// set the view engine to ejs
app.set('view engine', 'ejs');
And when referring to a view you should leave out the extension. So instead of res.render('index.ejs');
use res.render('index');
.
Setting up the view folder is not required since you're using the default view folder views
to store your view files.
Upvotes: 4
Reputation: 552
Did you set up the views folder?
app.set('views', './views')
Upvotes: 3
Reputation: 15639
You need to set your view
directory and your viewengine
before requesting any of your view files.
Hence you need to add the below lines, before your app.get
app.set('views', './views');
app.set('view engine', 'ejs');
And your res.render('index.ejs');
should be changed as,
res.render('index');
Hope this helps!
Upvotes: 21