Last active
February 9, 2024 18:45
-
-
Save abelsan/9b59f77b0a200ff458a133538813cc1b to your computer and use it in GitHub Desktop.
Templating in Node.JS - using ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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