expressjsHow do I use the Express.js template engine to create dynamic HTML pages?
Express.js template engine is a popular and powerful tool for creating dynamic HTML pages. It allows developers to use JavaScript code to generate HTML pages with dynamic content. Here is an example of how to use the Express.js template engine:
// Require the Express.js template engine
const express = require('express');
// Create an Express.js application
const app = express();
// Set the view engine to use the Express.js template engine
app.set('view engine', 'ejs');
// Define the view path
app.set('views', './views');
// Create a route to render the view
app.get('/', (req, res) => {
// Render the view with dynamic data
res.render('index', {
message: 'Hello World'
});
});
// Listen for requests
app.listen(3000);
The example code above will create a server on port 3000 that will render the view index.ejs
with the dynamic data { message: 'Hello World' }
. The view index.ejs
should look like this:
<h1><%= message %></h1>
The output of the example code above is an HTML page with the message Hello World
rendered in an <h1>
tag.
Parts of the example code explained:
const express = require('express');
- Require the Express.js template engine.const app = express();
- Create an Express.js application.app.set('view engine', 'ejs');
- Set the view engine to use the Express.js template engine.app.set('views', './views');
- Define the view path.res.render('index', { message: 'Hello World' });
- Render the view with dynamic data.
Helpful links
More of Expressjs
- How do I download a zip file using Express.js?
- How do I find Express.js tutorials on YouTube?
- How do I use Yarn to add Express.js to my project?
- How can I parse XML data using Express.js?
- How can I use an ExpressJS webhook to receive data from an external source?
- How can I use the x-forwarded-for header in Express.js?
- How do I set up a YAML configuration file for a Node.js Express application?
- How do I use Express.js to handle x-www-form-urlencoded data?
- How can I use Express.js to develop a web application?
- How do I use Express.js with W3Schools?
See more codes...