EJS / PUG

What is EJS?

EJS is a simple templating language that lets you generate HTML markup with plain JavaScript.

Installation

npm install ejs

app.js or index.js

add this code below const app or index

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

Default EJS folder

By default, Express will look inside of a views folder when resolving the template files, which is why we had to create a views folder.

Now, inside views folder create a file called index.ejs.

And the following into index.ejs:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

First, copy the following into app.js or index.js:

const express = require('express'); //Import the express dependency
const app = express();              //Instantiate an express app, the main work horse of this server
const port = 5000;                  //Save the port number where your server will be listening

app.set("view engine", "ejs");
app.get('/',(req,res,next) =>{
    res.render('index');
})

app.listen(port, () => {            //server starts listening for any attempts from a client to connect at port: {port}
    console.log(`Now listening on port ${port}`); 
});

If you run node app.js or index.js on the terminal from the root folder, then visit http://localhost:5000/, you should see the following results:

Last updated

Was this helpful?