expressjsHow do I use an Express.js HTML template?
Using an Express.js HTML template is a simple process. Here is an example of how to do it:
// Require the Express.js module
const express = require('express');
// Create an Express.js app
const app = express();
// Set the view engine to use HTML
app.set('view engine', 'html');
// Set the path to the HTML template
app.set('views', __dirname + '/views');
// Render the HTML template
app.get('/', (req, res) => {
res.render('index');
});
// Start the Express.js server
app.listen(3000, () => {
console.log('Listening on port 3000!');
});
Code explanation
const express = require('express');
- require the Express.js moduleconst app = express();
- create an Express.js appapp.set('view engine', 'html');
- set the view engine to use HTMLapp.set('views', __dirname + '/views');
- set the path to the HTML templateres.render('index');
- render the HTML templateapp.listen(3000, () => {
- start the Express.js server
Helpful links
More of Expressjs
- How can I use express-zip js to zip and download files?
- How do I implement CSRF protection in an Express.js application?
- How do I set the time zone in Express.js?
- How do I find Express.js tutorials on YouTube?
- How can I create and use models in Express.js?
- How do I set up a YAML configuration file for a Node.js Express application?
- How can I use the x-forwarded-for header in Express.js?
- How do I manage user roles in Express.js?
- How do I use Zod with Express.js?
- How do I render a template using Express.js?
See more codes...