How to send different HTML files based on query parameters in Node.js ? Last Updated : 22 Apr, 2021 Comments Improve Suggest changes Like Article Like Report The following approach covers how to send different HTML files based on the query parameters in Node.js Approach: The task can be easily fulfilled by receiving the query parameters and map different HTML files with the respective parameters. Step 1: Create your project folder and install the express module using the following command: npm install expressStep 2: Create a main.js file in the root directory of your project and write down the following code in it. main.js var express = require('express'); var app = express(); const fs = require("fs"); // Helper function to read and serve html files // according to the requested paths function readAndServe(path, res) { fs.readFile(path, function (err, data) { res.end(data); }) } // Setting get request path to receive id as query parameter app.get('/:id', function (req, res) { console.log(req.params); // Mapping different id's with respective html files if (req.params.id == "id1") readAndServe("./id1.html", res); else if (req.params.id == "id2") readAndServe("./id2.html", res); else { res.end("Invalid request"); } }); app.listen(8080, () => { console.log("Server Listening on Port 8080") }); Step 3: Create id1.html and id2.html files in the root directory of your project as shown below. id1.html <!DOCTYPE HTML> <html> <head> <title>Id 1</title> </head> <body> <h1>Requested Id = 1</h1> </body> </html> id2.html <!DOCTYPE HTML> <html> <head> <title>Id 2</title> </head> <body> <h1>Requested Id = 2</h1> </body> </html> Step 4: Run the server using the following command from the root directory of the project: node main.js Output: Now open your browser and go to http://localhost:8080/id1, you will see the following output when id1 is passed as a query parameter. Now go to http://localhost:8080/id2, you will see the following output when id2 is passed as a query parameter. Comment More infoAdvertise with us Next Article How to send different HTML files based on query parameters in Node.js ? A anshubajaj911 Follow Improve Article Tags : Web Technologies Node.js Node.js-fs-module NodeJS-Questions Similar Reads How do you access query parameters in an Express JS route handler? Express JS is a popular web framework for Node JS, simplifies the development of web applications. One common task is accessing query parameters from URLs within route handlers. In this article, we'll explore the fundamentals of working with query parameters in Express JS and demonstrate how to acce 2 min read How to Separate Routers and Controllers in Node.js ? In a Node.js application, especially when using the Express framework, separating routers and controllers is a common practice to keep the codebase organized, maintainable, and scalable. This separation of concerns ensures that routing logic is kept separate from the business logic, making the appli 4 min read How to Render Plain Text of HTML in Node.js ? Rendering plain text from HTML in Node.js can be useful for various applications, such as generating plain text email content, creating summaries, or extracting data from web pages. This article will guide you through the steps to achieve this using Node.js. Express Js is the web application framewo 2 min read How to Create a Pre-Filled forms in Node.js ? Pre-Filled forms are those forms that are already filled with desired data. These are helpful when a user wants to update something like his profile, etc. We 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 Filename: Sa 2 min read How Are Parameters Sent In An HTTP POST Request? HTTP POST requests are widely used in web development to send data from a client to a server. Whether you're submitting a form, uploading a file, or sending JSON data via an API, understanding how parameters are sent in an HTTP POST request is important. In this article, weâll explore how are parame 4 min read Like