Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
ISYS1101/ 1102 Database Applications
Week 11: Tute/Lab – Web Database Applications with node.js
Semester 2 2024
1 Objective
The objective of this tute/lab session is to learn the basics of combining MongoDB and javascript frameworks such as node.js, express etc to build a fullstack web application.
Node.js is popularly being used in web application development because it lets the application run while it is fetching data from the backend server. It is asynchronous, event-driven and helps to build scalable web applications. In this lab session, we explore the elements of MERN stack which is widely used in the industry.
What is MERN framework?
MERN is a pre-built technology stack based on JavaScript technologies. MERN stands for MongoDB, Express, React, and Node, after the four key technologies that make up the stack.
• MongoDB — document database
• Express(.js) — Node.js web framework
• React(.js) — a client-side JavaScript framework
• Node(.js) — the premier JavaScript web server (runtime)
MERN stack is an easy to learn, easy to implement full technology stack, which is considered to be a preferred full stack for beginners. MERN stack requires developers to have knowledge about only one programming language, i.e. JavaScript. Since MERN stack has a well-defined structure, developers can start building applications quickly and easily.
Express and Node make up the middle (application) tier. Express.js is a server-side web framework, and Node.js is the popular and powerful JavaScript server platform. MERN is the ideal approach to working with JavaScript and JSON, all the way through.
MERN Architecture
The MERN architecture allows you to easily construct a three-tier architecture (front end, back end, database) entirely using JavaScript and JSON.
Important Note: In this lab session we focus mainly on MongoDB, Node.js and Express.js components due to time constraints. However, you are encouraged to extend the lab activities to include React.js to build the top tier of the stack.
Front End (top tier) with React.js
The top tier of the MERN stack is React.js, the declarative JavaScript framework for creating dynamic client-side applications in HTML. React lets you build complex interfaces through simple components, connect them to data on your back-end server, and render them as HTML.
React.js has great important modern web framework features such as support for forms, error handling, events, lists, and more.
Application Tier with Node.js and Express.js
The program logic is implemented using node.js and embedded express.js functionality. Express web framework allows you to handle GET and POST requests from the React.js
Node.js allows you to write backend (server-side) application logic in JavaScript. With the MongoDB Node.js driver, you can easily connect a MongoDB database to the application in a few simple steps. This driver lets you implement CRUD tasks required for database operations which in turn sent to database backend to be executed.
Database Tier with MongoDB
A MongoDB database, either hosted locally or on cloud, will form the database tier. It receives the CRUD requests from the application tier and returns documents (in JSON format) back to the requestor.
Once you successfully complete this Lab/ Tute session, you will be able to build a basic, but fully- functional Web Database Application. You will still require to learn further web programming skills to improve the usability and functionality of your web database application. Complete the LinkedIn Learning tutorials (refer to Pre-reading section), MongoDB University developer path
(https://learn.mongodb.com/learning-paths/mongodb-nodejs-developer-path), and other web resources to acquire these skills.
2 Preparation Tasks
2.1 Hosting your database on Mongo Atlas
In this lab session, you will use a sample database hosted on Mongo Atlas. If you haven’t done yet, you must completed Week 9 Lab Activities (Getting Started with MongoDB) and Week 10 Lab Activities (Using MongoDB).
2.2 Setting up Visual Studio Code as your coding environment
2.2.1 Install Visual Studio Code
Visual Studio Code (VS Code, in short) is a free coding editor that helps you start coding quickly. Use it to code in any programming language, without switching editors. Visual Studio Code has support for many languages, including Python, Java, C++, JavaScript, and more.
Download and install Visual Studio Code, downloadable from: https://code.visualstudio.com
Useful resources:
• Getting Started -- https://code.visualstudio.com/docs
• Node.js tutorial in Visual Studio Code -- https://code.visualstudio.com/docs/nodejs/nodejs- tutorial
2.3 Install and set up Node.js and NPM (Node Package Manager).
In order to complete this Lab Session and the Assignment 4, you will require Node.js and NPM (Node Package Manager) installed in your computer.
If you have completed a course previously that uses these technologies, you may have already installed them.
2.3.1 Check if they are already installed
Run the following commands on the interactive terminal on VS Code.
2.3.2 Installation
If you do not have them already installed, follow the Node.js official installation guide, available here: https://nodejs.org/en/learn/getting-started/how-to-install-nodejs
Choose the most recent stable version for your operating system. You may use a package manager or a pre-built binary for your operating system. We recommend using the package manager option. For Windows, you can use fnm (Fast Node Manager) and for Mac, you can use nvm (Node Version Manager).
Windows:
Mac:
Different operating systems may present different quirks, so, if you encounter any issues, please refer to original installation guide or search internet forums.
After the completion of the installation, close VS Code and re-open it to get the above modules activated on its interactive terminal. Then, check the versions as above 2.3.1.
3 Activities
3.1 A simple Node.js Application
Let’s start with a console application. Create a new directory on your computer, change to this directory and open VS Code.
Edit a new node.js file, say “hello.js” in this directory. Add this line and save it.
To run this console application, open the interactive terminal on VS Code, and run:
Did it work?
Now that you were able to run your first node.js console application, try out a few more node.js programs.
3.2 More Console-based Activities
After completing the “Hello World”, it is time to get yourself familiarised with basic language syntax. Try out all the activities given in the following pages:
3.2.1 Output to the command line using Node.js
https://nodejs.org/en/learn/command-line/output-to-the-command-line-using-nodejs
3.2.2 Accept input from the command line in Node.js
https://nodejs.org/en/learn/command-line/accept-input-from-the-command-line-in-nodejs
3.2.3 Asynchronous flow control
One of the strengths of Node.js is its asynchronous nature of operations. While it may not be important in console-based applications, it will become an important aspect of th
3.3 Web Application with Node.js and Express.js
In this activity, you build a minimalist web application using Express.js web framework. Create a new directory on your computer, change to this directory and open VS Code.
Edit a new node.js file, say “app.js” in this directory. Add the following program code and save it.
The objects “req” (request) and “res” (response) are responsible for handling the HTTP requests and responses in the web applications. Please read the explanation here:
https://www.geeksforgeeks.org/explain-the-use-of-req-and-res-objects-in-express-js/
for more information on the inner workings of this program.
To run this program, you will need to set up a few things.
3.4 Handling Form Inputs
Using web forms and use form inputs in running a web application is a very common task. In this activity, we learn how to write a Node.js application with Express. We will use both GET and
POST methods to communicate with the server.
3.4.1 Create a Project Directory and Initiate a New Project
3.4.2 Install required Drivers
If you created the required directories, initialise the project, install required drivers, your project
directory should look like this:
3.4.3 Create your Node.js Application
On VS Code, create a Node.js file, say index.js and copy the following code:
3.4.4 Create the Web Form
On VS Code, inside public directory, create an html file, say form.html and copy the following code:
A Database
Item
3.4.5 Run your Application
Did it work?
If not, why not?
You will notice that the index.js does not include a section to handle POST requests (i.e. what to do when the form is submitted and makes a POST request to the server.
To rectify this issue, kill the current running application, and add the following section of code to the end of the index.js file.
Then, run it again.
Does it work this time?
3.5 Fetching documents from a MongoDB database
In this activity, we learn how to fetch data (JSON documents) from a MongoDB database. We use mongodb library which allows us to establish a database connection from a node.js application and implements the mongo CRUD functions in Node.js.
We use sample “Movies” database with the document collection “Movie” . We have used this document collection during last week’s lab. If it doesn’t exist on your Mongo Atlas, add/ create it before this activity.
3.5.1 Create a Project Directory and Initiate a New Project
3.5.2 Install the Node.js Driver
3.5.3 Obtain MongoDB Atlas Connection String
In the previous labs, you were required to obtain the connection string required for Mongo Compass application. Similarly, you are required to obtain the connection string customised for Node.js applications.
Follow the steps outlined here: https://www.mongodb.com/docs/drivers/node/current/quick- start/create-a-connection-string/
Each of your connection string would be different, however, it has a similar format, including the username, password, cluster uri combined together with some additional directives.
e.g.: mongodb+srv://:@-
cluster.pkhgj.mongodb.net/?retryWrites=true&w=majority&appName=Santha- Cluster
3.5.4 Create your Node.js Application
On VS Code, create a Node.js file, say index.js and copy the following code:
3.5.5 Run your Application
3.5.6 Exercises
In the activity in Section , you learned how to write a Node.js application (using Express) to serve the results on a browser. Convert the above console-based application into web-based application.
Hint: You will be required to install both mongodb driver AND express driver at the initialisation stage.
3.5.7 More Exercises
Copy the files in the above working directory to a new directory, and complete the following tasks:
• Replace the Movies database with sample_airbnb database and collection with listingsAndReviews collection.
• Write a node.js program to find 3-bedroom apartments in Barcelona. Assume there are more than one such listing in the chosen location.
4 Putting things together – Full MERN stack (excl. React.js
component).
This is the final piece in the jigsaw puzzle!
In this activity, we learn how to put all of the components we built in previous activities --- using express, create and use web forms and use PUT method to send form inputs to the server, using mongodb driver and write CRUD functions to operate on Mongo Atlas --- together to build a
complete the MERN stack.
DISCLAIMER: The only component we do not use in this stack is the React.js to build front-end (presentation layer) web pages. In this exercise we simply use plain html, however, they can be generated in Javascript using React.js.
4.1.1 Create a Project Directory and Initiate a New Project
4.1.2 Install required Drivers
If you created the required directories, initialise the project, install required drivers, your project directory should look like this:
4.1.3 Create your Node.js Application
On VS Code, create a Node.js files and html files: server.js on home directory, index.html, index.js, add.html, add.js inside public directory.
Download copies of these files from Canvas and edit them to include correct Mongodb connection strings and any other desired changes.
4.1.4 Run your Application
Did it work?
5 Exercise -- “Search Movies by Director” Application
In Section 4, you build an application to insert a new “movie” document and browse the whole “Movie” document collection.
Make a copy of the project (including files in the both home directory and files in the public
directory). Make necessary changes to convert this copy into a new “ Search Movies by Director” project.
The application will present the user with a form with one field when it is called with the following URL:
https://localhost:3000/search.html
Once the form is duly filled with a valid Director Number (dirNumb in the database) and submitted, it will send a POST request to the node.js backend, which in turn uses mongodb library, forms an
mql query and fetches the matching “Movie” documents from the database.
If the user input (director number) is invalid, i.e. it does not exist in the database, an appropriate error message should be displayed.