In this tutorial we are going to see how you can upload files to a server using Node.js using JavaScript, which is very common. For example, you might want to upload an avatar, a thumbnail, a PDF file or any other image or file in various formats.
We are going to structure this tutorial in several parts. First we will create the project, then we will create the frontend code and finally the backend code for the server.
To understand this tutorial you will need some basic knowledge of the command line. If you have never used the command line before, see the following command line tutorial, which explains some basic commands for the most commonly used operating systems. You will also need to have both Node.js and the npm package manager installed.
Table of Contents
1. Creating the project
The first thing we are going to do is to configure and create the project. To do this, create an empty directory somewhere, access it via the command line and use the following command to create the project:
npm init
We only need to install two packages. First install express using the following command:
npm install express
Next we will also need a middleware for express called express-fileupload which we can use to manage the files that are sent to the server:
npm install express-fileupload
Next, configure the file that will create the express server. To do this, edit the package.json file, which should contain the line "start": "node index.js" in the scripts section to tell Node which file to run when we start the application:
{
"name": "ulpload-file-ajax",
"description": "Tutorial in which an ajax medium file is created",
"version": "1.0.0",
"dependencies": {
"express": "4.16.2",
"express-fileupload": "^1.1.7-alpha.3",
"request": "^2.88.2"
},
"scripts": {
"start": "node index.js"
}
}
Next, we create the index.js file in the root folder of the project and we add the necessary code for the creation of a basic server:
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const fileupload = require('express-fileupload');
const FileController = require('./controllers/FileController');
const app = express();
const fileController = new FileController();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(fileupload());
router.post('/ulpload-file', fileController.uploadFile);
router.use(function(req, res) {
res.status(404).json({
error: true,
message: 'Not Found'
});
});
app.use('/api', router);
app.use(express.static(__dirname));
app.get('/', function (req, res) {
res.render('index.html');
});
var port = 3000;
app.listen(port, function () {
console.log('Server', process.pid, 'listening on port', port);
});
module.exports = app;
What we have done is to create the server. At the top of the file we have made a require of the express-fileupload module, which is essential for uploading files.
We have included the FileController class, located in the /controllers/FileController.js file, although we haven't created it yet. In the uploadFile function of this controller is where we will add the code in charge of uploading the file.
As for the path that will redirect the request to the controller, we have defined the path /api/upload-file using the following function:
router.post('/upload-file', fileController.uploadFile);
We have added the /api prefix to the path using the following statement:
app.use('/api', router);
Please have a look at this file, as we will be creating the frontend code next.
2. Frontend JavaScript code
In this section we will add the frontend code in charge of sending the file from the user's system to the server. To start, create the index.html file in the root folder of the project. Next, edit the file you just created and copy and paste the following code, which we will explain later:
Ajax Node File Upload Tutorial
Ajax Node File Upload Tutorial
What we have done is to add a basic HTML5 template. We have also added an HTML input field, which is the field that the user will interact with to upload the file:
As you can see, we've linked to the /resources/js/scripts.js script just before the closing