Express.js File Upload
Hello! In this tutorial, we will learn about Express.js File Upload. More specifically, we will handle the file upload in the node.js application. To perform the file upload we will use a simple express middleware (named
express-fileupload) as an npm package.
1.1 Setting up Node.js
To set up Node.js on windows you will need to download the installer from this link. Click on the installer (also include the NPM package manager) for your platform and run the installer to start with the Node.js setup wizard. Follow the wizard steps and click on Finish when it is done. If everything goes well you can navigate to the command prompt to verify if the installation was successful as shown in Fig. 1.
2. Express.js File Upload
To set up the application, we will need to navigate to a path where our project will reside. For programming stuff, I am using Visual Studio Code as my preferred IDE. You’re free to choose the IDE of your choice.
2.1 Setting up dependencies
Navigate to the project directory and run
npm init -y to create a
package.json file. This file holds the metadata relevant to the project and is used for managing the project dependencies, script, version, etc. Add the following code to the file wherein we will specify the required dependencies.
To download the dependencies navigate to the directory path containing the file and use the
npm install command. If everything goes well the dependencies will be loaded inside the
node_modules folder and you are good to go with the further steps.
2.2 Creating the index controller
Create a controller file that will expose the endpoints responsible for showing the file upload index page to the user and perform the upload operation. The pre-requisite for the controller is that you will need a
uploads folder in the project directory to store the uploaded files.
2.3 Creating an index page
Create the index HTML page that will be shown to the user to perform the file upload functionality.
3. Run the Application
To run the application navigate to the project directory and enter the following command as shown in Fig. 4. If everything goes well the application will be started successfully on port number
4. Project Demo
When the application is started, open the browser of your choice and hit the following url –
http://localhost:5001. The file upload index HTML page will be shown to the user as in Fig. 3.
Select the file of your choice and on successful upload, the message will be sent to the user as shown in Fig. 4.
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
In this tutorial, we learned:
- Introduction to
- Steps to setup Node.js
- Sample programming stuff
You can download the source code of this tutorial from the Downloads section.
6. Download the Project
This was a programming tutorial to handle file upload in a node.js application.
You can download the full source code of this example here: Express.js File Upload