How to create helper functions with EJS in Express ? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Helper functions in EJS are JavaScrict functions that you can define and use within your templates. These functions can perform various tasks, such as formatting dates, generating dynamic content, or handling repetitive logic. By creating helper functions, you can keep your template code clean and organized, making it easier to maintain and understand. In this article, we will explore how to create helper functions with EJS in Express. Steps to create helper functions with EJS in Express:Step 1: Initialize the node application using the following command. npm init -yStep 2: Install the required dependencies: npm install express ejs nodemonFolder Structure: The updated dependencies in package.json file will look like: "dependencies": { "ejs": "^3.1.9", "express": "^4.18.3"} Example: Now create the required files as shown in folder structure and add the following codes. HTML <!-- views/index.js --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Formatting date</title> </head> <body> <h1> <%= message %> </h1> <p>Today's date is: <%= formatDate(new Date()) %> </p> </body> </html> JavaScript //app.js const express = require('express'); const app = express(); const port = 3000; app.set('view engine', 'ejs'); app.set('views', __dirname + '/views'); app.get('/', (req, res) => { res.render('index', { message: 'Formatting date using helper function in ejs' }); }); app.locals.formatDate = (date) => { if (!date || !(date instanceof Date)) return ''; const options = { year: 'numeric', month: 'long', day: 'numeric' }; return date.toLocaleDateString('en-US', options); }; app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); To start the application run the following command nodemon app.jsOutput:Formatting date using helper function in ejs Comment More infoAdvertise with us Next Article Express.js express.raw() Function R rahulshv8yy Follow Improve Article Tags : Web Technologies Node.js EJS-Templating Language View Engine Similar Reads How to use Global functions in Express JS? In this article, we will learn the global function of Express. Express JS is a web application framework for Node JS. This framework runs on the server-side framework. It is a trendy Express JS framework for building scalable web applications. There are many functions available in Express JS that ar 2 min read Create an Express server with EJS Templating Engine EJS (Embedded JavaScript) is a simple and popular templating engine for JavaScript. it is designed to generate HTML markup with plain JavaScript. EJS is commonly used in server-side web applications built with Node.js. It allows you to generate dynamic HTML pages. In this article, we will create stu 3 min read Express express.Router() Function The express.Router() function in Express.js creates a new router object that can handle requests in a modular and organized way. It serves as a mini-application with middleware and routes but is limited to specific segments of your application. By using express.Router(), you can organize your Expres 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.js express.raw() Function The express.raw() function is a built-in middleware function in Express. It parses incoming request payloads into a Buffer and is based on body-parser. Syntax:express.raw( [options] )Parameter: The options parameter contains various properties like inflate, limit, type, etc. Return Value: It returns 2 min read Express.js router.METHOD() Function The router.METHOD() method provides the routing functionality in Express, where METHOD is one of the HTTP methods, such as GET, PUT, POST, and so on, in lowercase. Syntax: router.METHOD(path, [callback, ...] callback) Parameter: The path parameter specifies the path on the URL and callback is the f 2 min read Like