Set the lookup path of view folder of ejs in express

I have app.js in Express_server folder.Full location of aap.js is I:\WEB Development\Node\Express_server\

and I made views folder at the location I:\WEB Development\Node\

How can I set the lookup path of views folder to above location(I:\WEB Development\Node)?

code of my app.js

const express = require('express');

const app = express();
app.set('view engine', 'ejs');
app.get('/',function(req, res){
    res.render('index');
});

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You can change your views default path:

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

Solution 2

const express = require('express')
const app = express()
const port = 3000

require("./route/route.js")(app);

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


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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply