![]() |
VOOZH | about |
In this article, we are going to build a simple Toll Road Management System using Node.js, where the data will be stored in a local MongoDB database.
Problem Statement: In a toll tax plaza, it is difficult to record all the transactions and store them in a single place, along with that, if required, it would be difficult to list the data of vehicles that crossed that toll plaza. We aim to build an app that allows the toll manager to add the toll receipts to our database, and all the receipts(details of the vehicles that crossed the toll) should be visible to the toll manager.
So, we are going to build our app using the following approach: We are going to build the front end using simple HTML(along with some CSS) and Bootstrap, and the backend would be in Node.js, and the database we are going to use is our local MongoDB Database.
Our app will basically contain these two pages, which we are going to build further in the article:
Here's what our app is going to look like:
So let's start building our App.
Installing the required packages in our application: Firstly, you must have basic knowledge about Node packages like "express", "body-parser", "ejs" and "mongoose", along with MongoDB installed in your pc. If you do not have a basic understanding of any of these packages or have not installed MongoDB yet, it is suggested that you refer to the article links provided. A brief description of all of them will be provided as we go through the code.
So, first of all, navigate to a new folder/directory, and create a Server.js file in it by executing the following command in the command line:
Now that we have created the server.js file, we need to initialize npm in our folder, using the following command:
Once npm has been initialized, we can install the required packages, which are, express, ejs, body-parser, and mongoose. Execute the following command in order to install all these packages at once.
Till here, the project structure should look like this:
Now, in the Server.js file, we will require all the packages we installed above, so we can later use them in our Server.js file. In the server.js file, we have to add the following code:
Server.js file:
Handling the /newReceipt route using express: Now, we are going to use express to handle the /newReceipt route of our app (which will allow the toll manager to fill in the details regarding the vehicle crossing the toll). First of all, we are going to allow our app to listen on a specific port, and then we have to create different routes to handle different requests. Here's how we are going to do it:
Server.js file:
Connecting the app to the database: Now that we have to set up our Mongo Database using the package called "mongoose" which we have installed above. For that, firstly, we have to connect mongoose to our local database using the code given below:
Server.js:
Here, the name of the database is taken as transactionDatabase (as it will store all the transactions). The code above will create a new database called "transactionDatabase" and will connect it to the Mongoose.
Now, we have to create the Schema for our Transaction, that is, in what format and with what details are we going to store our toll transaction? So, we are going to have 4 things in our record, i.e. Vehicle Number, Amount of toll tax paid, the Date, and Time of the record. So, here's how we are going to create a schema for this:
Server.js:
Here, in the transactionSchema variable, we have vehicleNumber, date, and time which are of type String, and the tollAmount is of the type Number. So, this is the schema of our transaction that we are going to use to store in our database.
Now that we have created the schema, we have to create a model for the same, which will allow us to create an instance of this schema.
The following code will allow us to make Model using our Schema which we can later use to create instances.
Server.js:
Our model will be named "Transaction", and the schema it uses is transactionSchema, which we created above.
Now we are ready to create a new Transaction (which has the detail of vehicleNumber, tollTaxAmount, date, and time) using our Model, but for that, we require the data related to the vehicle crossing the toll which is to be recorded by the Toll Employee. For that, we have to create an HTML page, which will allow us to take the inputs related to the vehicle number, toll amount, etc.
Creating an HTML file to take inputs regarding toll transactions: We are using Bootstrap in this so as to make things more clean and tidy, so make sure to add Bootstrap CDN in the HTML file.
Create a file called newReceipt.html, and add the following code to it.
NewReceipt.html:
The explanation of the above code is given in the points below:
Now we have created this simple HTML page named newReceipt.html, we have to make sure that when the app.get("/newReceipt") gets called, we can serve this page as a response. For a better understanding, look at the code below:
Serving the NewReceipt.html file on the home("/") route:
Server.js:
This code above has to be added in the Server.js file, which will serve this newReceipt.html created above as a response when we go on the /newReceipt route.
So, let's check whether everything is working fine or not by starting the server and going to the /newReceipt route. For that, start the server first using the command shown below:
Make sure that the server is started. Now, type "localhost:8080/newReceipt" in the address bar of your web browser, and you will see the HTML page that we created above displayed in the web browser. The process is shown below:
Till now, if everything is working fine, we can proceed further to create a new Transaction and store that in our transactionDatabase. For that, we are going to take the data from the form element that we created in the "newReceipt.html".
Fetching data from the form created in the "NewReceipt.html" file and adding data to our database: In order to read the form data, we will be using the package called "body-parser", which we had installed above. In order to use this, we have to first tell our app to use bodyParser, which can be done by adding a line just below the require package line in the Server.js file:
Server.js:
The form element in "newReceipt.html", on submission, a post request will be generated to the /newReceipt route, which we have to handle in the Server.js file, as shown in the code below:
The explanation of the code is given below:
Now, once we submit our form data, by clicking on the button in the form, we will save a new transaction instance in our transaction database.
Let's check the progress till here. Once again we will start the server, using the command shown below:
Once the server is started, we can go on to the /newReceipt route, where we get "newReceipt.html" as a response. Once we enter vehicleNumber and amount, and click on the button, we will see a response "saved" stating that the data has been saved. A demonstration of this is given below:
Now that we have saved the transaction in the database, we have to show these transactions as well. For that, we will create a get request for the home route, which will show all our records in a tabular format. For that, we have to add the following code in the Server.js file:
Sending data from Server.js file to our home route after fetching from the database:
Server.js:
In order to get the data from our transactionDatabase, we have to use the Mongoose package, and the function we are going to use is find(), which will take two parameters, one in the condition(no condition in this case because we want to fetch all the records) based on which data is fetched from the database, and second will be the callback function. The code below demonstrates this:
Server.js:
In the code above, a few points to be noted are:
So, let's test it again. Restart the server, using the command "node server.js"(shown in the GIF image below), and then go on the home route, and you will see a response like this:
Till here, we have got the data from our transactionDatabase on the home route, but the data is in the raw JSON format.
Showing the data in a systematic manner: Now, our final task is to show that data in a systematic manner in tabular form. For that, we are going to use the package called "ejs", which we have already installed above, which basically allows us to use the variables of Server.js file in the HTML. For that, firstly we have to create a "views" folder in our directory, and then create a new "index.ejs" file inside that folder. One thing to note here is, that we can write simple HTML in our ejs file as well. So, let's start by adding simple HTML content to our index.ejs page.
The starting code for index.ejs file is given below (which is basic HTML):
Index.ejs:
Now, while handling the get request on the home route, we have to pass this index.ejs file, along with the data that we have fetched from our transactionDatabase. For that, while handling the get request on the home route, we have to make the following change in the home route:
Instead of using res.send() method, we are going to use the res.render() method, in which we are going to pass two parameters, first which is the ejs file we are going to send as a response, and second will be the data that we want to send to the ejs file.
For that, the code is given below:
Server.js:
The following things are to be notes related to the code written above:
Now, in the index.ejs file, we have to create tables and use ejs to properly arrange the data that we have got from the Server.js file.
For that, firstly we have to create the table element, and in that, we are firstly going to add the table headers using the code given below:
Index.ejs:
Some important points to note related to the code given above:
Before you head over to the next step, you must have enough knowledge about the ejs package of Nodejs. If not, you can check out the article here.
Adding Javascript to Index.ejs file: Now, we are going to use Javascript in HTML code, or the index.ejs file, which is what ejs allows us to do.
Here's what we are going to do:
Here's how we are going to do it:(Only the code for table body tag, i.e., <tbody> is given below):
Index.ejs:
The HTML above seems a bit odd because we have used Javascript inside it, to create for loop and used Javascript variables as well. Here's the explanation of the code above:
One last thing to be done here is, that we must have a "Add Transaction" button on our home page, or our transactions page, clicking on which, we can generate a new transaction. One simple form element is all we need in our index.ejs file. The code for that is shown below:
Index.ejs:
Now that we have completed our index.ejs file, we are done with our app and now we are going to test it one final time. So, restart the server using the "node server.js" command as shown in the image below:
And the result of our app is shown below:
Complete code: The entire code for Server.js file and the NewReceipt.html file is given below:
Conclusion: We can now record the transactions of the vehicles passing through the toll including the vehicle number, date, time, and the amount for the toll using the app. Along with that, all the toll transactions will be listed together in one place on our home page. Building a toll road management system can greatly improve the efficiency and effectiveness of toll collection, traffic management, and overall road safety.