Express.js app.engine() Function
Last Updated :
08 Jan, 2025
The app.engine() function is used to register the given template engine callback as ext. By default the Express itself will require() the engine based on the file extension.
Syntax:
app.engine(ext, callback)
Parameters: The ext parameter is the extension type like ejs, hbs, etc and callback is the function passed as a parameter.
Return Value: It returns an Object.
Installation of the express module:
You can visit the link to Install the express module. You can install this package by using this command.
npm install express
After installing the express module, you can check your express version in the command prompt using the command.
npm version express
After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.
node index.js
Project Structure:

Create a views folder and put a home.html file in it with the following code:
Example 1: Filename: home.html
html
<html>
<head>
<title>app.engine() Demo</title>
</head>
<body>
<h2>EJS Engine</h2>
</body>
</html>
For example, to map the EJS template engine to “.html” files:
app.engine('html', require('ejs').renderFile);
Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.engine('html', require('ejs').renderFile);
app.get('/', function (req, res) {
res.render("home.html")
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
Make sure you have installed the express and ejs modules using the following command:
npm install express
npm install ejs
Run the index.js file using the below command:
node index.js
Output:
Console Output:
Server listening on PORT 3000
Browser Output:
Now open your browser and go to http://localhost:3000/, you can see the following output on your browser:
EJS Engine
Example 2: Filename: home.html
html
<html>
<head>
<title>app.engine() Demo</title>
</head>
<body>
<h2>Handlebars Engine</h2>
</body>
</html>
Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
console.log(app.engine('html', require('hbs').renderFile));
app.get('/', function (req, res) {
res.render("home.html")
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
Make sure you have installed the express and hbs modules using the following command:
npm install express
npm install hbs
Run the index.js file using the below command:
node index.js
Output:
Console Output:
Server listening on PORT 3000
Browser Output:
Now open your browser and go to http://localhost:3000/, you can see the following output on your browser:
Handlebars Engine
Reference: https://expressjs.com/en/4x/api.html#app.engine
Similar Reads
Express app.use() Function The app.use() function in Express.js adds middleware to the application's request-processing pipeline. It applies the specified middleware to all incoming requests or to specific routes, allowing you to modify request/response objects, perform operations, or handle errors throughout the application.
3 min read
Express app.post() Function The app.post() function in Express.js handles HTTP POST requests to a specific route. It defines a callback function to process incoming data sent via POST, typically used for submitting forms or sending data to a server from clients.Syntax:app.post(path, callback [, callback ...])Arguments:Path: Th
2 min read
Express res.end() Function The res.end() function concludes the response process and is derived from the HTTP.ServerResponse's response.end() method in the Node core. It is employed to promptly conclude the response without including any data.Syntax: res.end([data] [, encoding])Parameters: The default encoding is 'utf8' and t
2 min read
Express app.listen() Function In Express.js, the app.listen() function is used to start a server and make it listen for incoming requests on a specified port and host. This method is essential for running an Express application as it enables the server to handle HTTP requests from clients (like browsers).Syntax: app.listen([port
3 min read
ExpressJS express.json() Function The express.json() function is a built-in middleware in Express that is used for parsing incoming requests with JSON payload. The express.json middleware is important for parsing incoming JSON payloads and making that data available in the req.body or further processing within the routes. Without us
4 min read
Express app.get() Request Function The app.get() function is used to define routes on your server that handle HTTP GET requests. A GET request is typically used when the client asks the server to send back some information, like retrieving a webpage or data from a database.Itâs an essential part of building websites and APIs, as it a
6 min read