Skip to content

Instantly share code, notes, and snippets.

@abelsan
Last active February 9, 2024 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abelsan/9b59f77b0a200ff458a133538813cc1b to your computer and use it in GitHub Desktop.
Save abelsan/9b59f77b0a200ff458a133538813cc1b to your computer and use it in GitHub Desktop.
Templating in Node.JS - using ejs
<!-- Inside your project directory, create a folder named "views". -->
<!-- Inside the "views" folder, create a file named "index.ejs". -->
<!-- Add the code below to "index.ejs", this is your EJS template -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World Example</title>
</head>
<body>
<h1><%= message %></h1>
</body>
</html>
// Install express and ejs with command below
// npm install express ejs
const express = require('express');
const app = express();
// Set the view engine to ejs
app.set('view engine', 'ejs');
// Define a route for the root path
app.get('/', (req, res) => {
res.render('index', { message: 'Hello, World!' });
});
// Start the server on port 3000
app.listen(3000, () => console.log('Server is running on http://localhost:3000'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment