0

Just to preface: I've recently been transitioning my web development skills from PHP + HTML/CSS to Nodejs, but have been having trouble with templating.

I'm using Express, and after some research tried to use Pug.js, but found that too complicated for what I'm trying to achieve at the moment, so I moved to ejs as it seemed to be more simple.

What I'm trying to get is to be able to reuse HTML that I have already written for multiple pages on the site - which from what I've found is called 'partials'?

Now to the code I've written so far:

server.js:

var express = require('express');
app = express();
publicFolder = __dirname + '/public';

app.set('view engine', 'ejs');

app.use(express.static(publicFolder, {
    extensions: ['html']
}));

app.listen(80, () => {
    console.log('Server is online on port 80')
});

index.html: (/public/index.html)

<html>
    <body>
        <% include ../views/header.ejs %>
    </body>
</html>

header.ejs: (/views/header.ejs)

<h1>Test header</h1>

I understand I may be on the completely wrong track, but any help would be greatly appreciated.

Regards

EDIT: I've double checked to make sure it's not a path issue

Chase
  • 3,009
  • 3
  • 17
  • 23
Josh B
  • 1
  • 2

1 Answers1

0

It might be easier for you to store all your view files as ejs in the same folder and user the path module to set your static files.

server.js:

var express = require('express');
var path = require('path')
app = express();

app.use(express.static(path.join(__dirname, 'public')));

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

app.listen(80, () => {
    console.log('Server is online on port 80')
});

index.ejs

<html>
    <body>
        <% include header %>
    </body>
</html>

Just a consideration. Hope it helps!

JazzBrotha
  • 1,628
  • 11
  • 15