CRUD Operations and File Upload using Node.js and MongoDB
Within the computer programming world, CRUD is an elision for the 4 operations Create, Read, Update and Delete. Create, Read, Update, and Delete (CRUD) are the four basic functions that models should be able to do, at most. In RESTful applications, CRUD often corresponds to the HTTP methods like POST, PUT, DELETE, and GET respectively. Each database requires some functions to be called so that users can perform some operations. These operations are used by the users to perform certain types of queries on a particular data within the database. The basic of the operation is CRUD operations. CRUD operations and the File Upload will be done with the help of Node js framework and its packages along with MongoDB and Postman.
Before deep-diving into how the code works let us know the prerequisites of the applications. The local machine should have Node js, Postman and MongoDB installed as the prerequisites.
- Node js (npm): If one doesn’t have Node.js and npm install go to the official link to install Node.js and run the following command on the terminal for downloading npm.
npm install npm@latest -g
- Postman: Postman is used to manage the requests. It helps in testing the HTTP requests, with the help of GUI without copying and pasting each HTTP request everywhere. Go to the official link to download PostMan for free.
- MongoDB: MongoDB is the database that is used in CRUD operations. If you don’t have a MongoDB account, don’t worry the article will be giving step-by-step instructions for it too.
This project is required to be built in node js with MongoDB as the backend. It provides the following capability through the API interface:
- CSV File Upload from Multipart Form
- Scan the uploaded CSV and push its content into MongoDB collection
- API to perform CRUD operations on the above collection
The above API are using a passport package for basic authentication to manage access
Framework and Runtime Environment Used: Here are the major frameworks and run time environments that are used in the project.
- Node js
- NPM package Manager
- Mongo DB
Step 1: Make the Node.js project. To do that write down the following command in the terminal.
After writing the code into the terminal you need to fill out the information. To make it default press ENTER at each section. At the end, you will get a filed named package.json in your directory.
Some of the packages as dependencies are used in this project namely – express, express-session, bcryptjs, fast-csv, csvtojson, dotenv, multer, passport, passport-local, and mongoose. Run the command to install these packages.
Step 2: Set up a server. To set up the server for the application, make 2 files named app.js and .env. app.js will act as the server file and .env will help in loading the environment variables.
Inside the .env file write port number for your localhost PORT = “Port Number” and in app.js write down the following code to run up the server.
To access the routes folder where all the requests are given write the following line –
The final code for now in app.js will look like this –
Step to run the application: Open the terminal and writing node app.js will start the server.
Step 3: Connect to MongoDB collection. If one doesn’t know how to connect to MongoDB go through the following steps, otherwise skip the following steps –
- Go to MongoDB Atlas
- Set up an account
- Build a new project
- Build a database (it might take a couple of minutes)
- Go to database access and fill out the username, password, and IP address section (You can five your current IP address as default)
- Click connect and choose to connect your application and it will give you an address. Copy the address and paste it inside .env file
The final .env file will look something like this –
DB_CONNECT = "YOUR PATH" PORT = "Port Number"
After making a collection connect the Node.js application with MongoDB by writing the following code in app.js.
Step 4:This step describes the project flow structure. The project is divided into the various section. There is a middleware Folder along with the controller, routes, and config. In the _others_ folder there is a dummy CSV file for upload. Upon using the routes and uploading it, the CSV file is uploaded in uploads folder. Make the following files in each section.
NOTE: The csv_controller.js and csv.js are just programs for CRUD Operations.
NOTE: No need to make csv copy.js as it is just a copy csv.js.
Schema: To put it simply, the schema is a skeleton structure of the whole database. It allows us to define various fields for users to enter.
Step 5: Make a schema for the user and for crud operation. Since the data needs to be dynamic we will be defining the schema for the database.
Under models: User.js
A schema with the name User is created that accepts name, email, password, and date as its fields.
Within crud.js a schema with accepting fields username, identifier, firstName, lastName, updated is created.
Step 6: Having a proper authentication and authorization is very important in every organizations. For that each organizations put a layer of security. The files auth.js and passport.js are doing just that. But since the article helps in performing CRUD operations and file upload we will not be deep diving into it.
The auth.js file see whether the user has logged into the account. If yes, the user is authorized to view the page otherwise it is redirected to the home page
Along with these 2 files, there is a need for session variable and authorization to include passport into this project. To do so, app.js is opened and passport middleware and session is initialized.
Step 7: A middleware is made using with the help of multer package. To use the multer package write the following command in the terminal.
npm install --save multer
The package multer helps in handling multipart/form-data, which is primarily used for uploading file. Multer adds a body object and a file or files object to the request object. The body object contains the values of the text fields of the form, the file or files object contains the files uploaded via the form.
Step 8: After the authentication is done with passport and multer as middleware is added into the project, the routes and the controller for the file upload and CRUD operations are needed to be completed.
Before jumping into the operations first the routes are needed to be initialized. To do so, index.js is used.
To use the passport authentication the routes and controller for the user are made in users.js and user_controller.js.
It makes the route for the register and login as a post method and a logout system as a get method. Inside the user_controller.
The register exporter first checks whether the credentials given are correct or not. If they are correct, then the password is encrypted with the help of bcrypt . And after that a new user is saved inside the schema. To check whether the function works or not a request is sent through with the help of postman for register, login and logout.
Register: Sending a post request http://localhost:3000/register with the help of postman.
After the registration is successful, the same user can be seen in MongoDB atlas.
Login: Sending a post request http://localhost:3000/login
Logout: Logout send out a get request.
Crud Operations and File Upload: Using the files csv.js and csv_controller.js, all the CRUD operations and the file upload will be performed. Various API’s are called which help in achieving various functions. Like http://localhost:3000/fileUpload helps in uploading the CSV file and pushing the data into Mongoose. Similarly, http://localhost:3000/CRUDcreate, http://localhost:3000/CRUDread, http://localhost:3000/CRUDupdate, http://localhost:3000/CRUDdelete helps in other CRUD operations CREATE, READ, UPDATE, and DELETE respectively.
Create: We will be sending out the post request. As the name signifies, they create functions all the users to enter a new record in the database. For a relational database, a create function is called upon known as INSERT. In non-relational databases like MongoDB provides two different options to insert documents into the collection – db.collection.insertOne()
Once the operation is done after adding the necessary packages in the files, .save() is used to save the information to the database.
Read: The read function in the CRUD operation is similar to a search function. What it does it helps the user to search and retrieve particular information of a particular data. In MongoDB we use db.collection.find() or db.collection.findOne() to retrieve the information.
Update: The update function is used to modify the data present in the collection of the MongoDB database. TO change a record users may have to modify multiple fields. This requires carefully updating the documents as the changes done while updating the collection is permanent and irreversible. 3 different methods can be used in MongoDB for updating – db.collection.updateOne(), db.collection.updateMany(), db.collection.replaceOne().
Delete: The last CRUD operation is Delete. As the name suggests it help in deleting a single collection from a document.
File Upload: As discussed before file upload is done with the help of package multer.
Please Login to comment...