![]() |
sendFile() function in Express.js is mainly used to send the file at the given path whereas the render() function is used to render a view and send the HTML string to the client as a response. In this article, we will see the detailed difference between this function with their syntax and practical implementation. Prerequisites:Table of Content Steps to Create Express application:Step 1: In the first step, we will create the new folder by using the below command in the VScode terminal. mkdir folder-name Step 2: After creating the folder, initialize the NPM using the below command. Using this the package.json file will be created. npm init -y Step 3: Now, we will install the express dependency for our project using the below command. npm i express ejs The updated dependencies in package.json file will look like: "dependencies": { What is sendfile() function?The sendFile() function in Express.js is used to send the file in response to the HTTP request which is done by the client. This function takes the absolute path of the file as the argument and the file which is used to be sent to the client. Syntax: res.sendFile(path [, options] [, fn]) Example: Below is the example for the sendFile() function.
Output: What is render() function?The render() function in Express is used to generate the dynamic HTML views. It mainly renders specified template files by combining them with the data and facilitating thw creation of dynamic web pages in response to the HTTP request of the clients. Syntax: res.render(view, [locals], callback); Example: Below is the example for the render() function.
Output: Difference between Express sendfile() and render():
|
Reffered: https://www.geeksforgeeks.org
Express.js |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |