Node.js sends files to the client
In this article, we will learn how to send files to the client in Node.js using Express.
Sending files using Express in Node.js
Express.js or Express is a backend web utility framework for Node.js. Express is a Node.js web application framework that provides a powerful feature set for web and mobile applications.
The function res.sendFile() passes the file in the specified path and sets the HTTP header field of the Content-Type response according to the file extension.
grammar:
res.sendFile(path [, options] [, fn])
parameter | illustrate |
---|---|
path | A mandatory parameter describing the path to the file to be sent. |
options | An optional parameter containing various properties of the file being sent, such as maxAge, root, etc. |
fn | A callback function is called when a file is created. |
Follow the instructions below to send the file to the client.
Full source code - index.js :
const express = require('express');
const app = express();
const path = require('path');
const PORT = 3001;
app.get('/', (req, res, next) => {
const fileName = 'helloworld.txt';
res.sendFile(fileName, {root: path.join(__dirname)}, (err) => {
if (err) {
next(err);
} else {
console.log('File Sent:', fileName);
}
});
});
app.listen(PORT, (err) => {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
In the above example, we created a 3001
server that listens to the specified port. Once the server listens to the specified port, it will execute the code in the first matching route.
sendFile()
The response object will then be returned to the client
via the method. If any errors are encountered, it next()
will pass the errors to the error handler using the method.
If everything goes well, it returns a response object with the file contents to the client.
Try running the above code in replit with Node.js support , it will display the following result.
Output:
Server listening on PORT 3001
File Sent: helloworld.txt
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Throwing Errors in Node.js
Publish Date:2025/04/17 Views:164 Category:Node.js
-
This article will explain how to throw errors in Node.js. Throwing Errors in Node.js Errors are statements that do not allow the system to function properly. Errors in Node.Js are handled through exceptions, which are created with the help
Solve the Cannot Find Module error in Node.js
Publish Date:2025/04/17 Views:70 Category:Node.js
-
In this article, we will learn how to fix the Cannot find module error in Node.js. package.json File Before diving into the solution, we will first try to understand the package.json file and why we need it. The package.json file is the roo
Multithreading in Node.js
Publish Date:2025/04/17 Views:112 Category:Node.js
-
In Node.js, the term multithreading does not apply because Node.js is designed to run in a single-threaded event loop. However, Node.js is built on top of the JavaScript language, which is single-threaded by default. However, Node.js provid
Using jQuery in Node.js
Publish Date:2025/04/17 Views:51 Category:Node.js
-
jQuery is a popular JavaScript library that is widely used to build web applications. It provides a rich set of APIs for interacting with the DOM, making HTTP requests, handling events, etc. Node.js is a JavaScript runtime that allows devel
Node.js 发送文件到客户端
Publish Date:2023/03/27 Views:341 Category:Node.js
-
在本文中,我们将学习如何在 Node.js 中向客户端发送文件。