Mostafa Ghadimi
Mostafa Ghadimi

Reputation: 6786

Loading .ejs file failed using Node.JS

I have searched many articles and posts asked in different sites, but I didn't find any proper answer. I'm using ejs in my project to render book details in my page.

My project structure looks like:

  • asset > ...

  • server > server.js

  • views > main > bookShow.ejs

I've used the ejs library as the following piece of code in server.js:

var ejs = require('ejs');
app.set('view engine', 'ejs');

and I've returned the bookShow.ejs in GET request as:

router.get('/bookShow/:isPageNoSorted/:page', (req, res) => {
    var isPageNoSorted = req.params.isPageNoSorted || false;
    var page = req.params.page || 1;
    var perPage = 5;
    if (isPageNoSorted == "true") {
        bookModel.find({
                isDeleted: false
            }).sort('-pageCount')
            .skip((perPage * page) - perPage)
            .limit(perPage)
            .exec((err, data) => {
                if (data) {
                    if (data.length > 0) {
                        bookModel.find({
                            isDeleted: false
                        }).count().exec((err, count) => {
                            res.render(
                                'main/bookShow', {
                                    books: data,
                                    pageCount: count,
                                    currentPage: page
                                }
                            );
                        })
                    } 
                }
            })
    } 
});

but nothing is shown in the browser. It seems to be OK and I have seen another sample that works in another site. Is there any possible way that put my files in arbitrary directory using express middleware? For example my project structure is like:

assets > views > bookShow.ejs

Upvotes: 0

Views: 84

Answers (1)

user10343492
user10343492

Reputation:

try the following code to use it:

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

Upvotes: 1

Related Questions