Centell
Centell

Reputation: 409

Ejs include with routed url

The files header.ejs,menu.ejs,footer.ejs are in views/layouts/basic/.

I want to include these files in the views/index.ejs.

So, I tried:

<% include ./layouts/basic/header.ejs %>

It works well. But A lot of view files were created, and the relative paths were all different.

So, in app.js:

app.use('/layout', express.static(__dirname + '/views/basic/layouts'));

and index.ejs:

<% include /layout/header.ejs %>

but it doesn't work. It occurs

 ENOENT: no such file or directory, open '/layout/header.ejs'

However, header.ejs is downloaded when you access the browser with 'http://localhost:3000/layout/header.ejs'.

Why doesn't it work? and how can I fix it?

Upvotes: 0

Views: 574

Answers (1)

MjZac
MjZac

Reputation: 3536

<% include /layout/header.ejs %> is evaluated in server side, hence it looks for absolute path in the disk, rather than a web url. When you make http request to localhost:3000/layout/header.ejs, the file is served.

You can use https://www.npmjs.com/package/express-ejs-layouts to create various layouts and use it with different views.

Upvotes: 1

Related Questions