Build Backend Servers Api With Node and Express

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

build backend servers api with node and express (1)

By

Mohammed Taha Hamad Ali


Ali Kazem Jaweed Hajul
Laith Sabah Akab Hassan
Younis Ghanem Khalaf Abd

Supervisor
Asst. Lect. Hamid Abdul Rahman
A thesis submitted in partial fulfilment of the requirements for the degree of

Computer Engineering Techniques Department

Technical College

Imam Ja'afar Al-Sadiq University

BSc

March 2024

Kirkuk, Iraq
Index
Number of page Subject
1 Introduction
2 History
2 Node.js
4 Advantages of NodeJS
6 Disadvantages of using Node.js
7 Create a backend API in Express JS
9 What is a Server
9 Uses of Servers
10 Server Component
11 Server Works
11 Types of Servers and Their Applications
14 Why Are Servers Always On?
15 Server Maintenance Plan
16 Server Maintenance Steps To Success
16 Common Server Problems & How To Fix Them
20 Conclusion
22 Reference
Introduction

A server is a hardware device or software that processes requests sent over


a network and replies to them. A client is the device that submits a request
and waits for a response from the server. The computer system that accepts
requests for online files and transmits those files to the client is referred to
as a “server” in the context of the Internet.

A Server is a program or a device that provides functionality for called


clients which are other programs or devices. This architecture is called the
client-server model. a single overall computation is distributed across
multiple processes or devices. Servers can provide various functionalities
called services. These services include sharing data or resources among
multiple clients or performing computations for a client. Multiple clients
can be served by a single server, and a single client can use multiple servers.

The development of backend servers and APIs is a critical aspect of modern


web application architecture. It involves handling server-side logic,
database interactions, and API endpoints to facilitate communication
between the frontend and backend. Node.js, known for its efficiency and
scalability, paired with the simplicity and flexibility of Express.js, has
become a popular choice for .building these server-side components

All the APIs on the Bonita platform are developed in JAVA which is a
backend programming language, we found Node.js and Express more
convenient , in terms of learning and grasping the basics of it quickly.
Building an API is a challenging job that makes we feel that we am moving
out of comfort zone. In this project, we going to share how we built API
with Node.js and Express, We will explain the servers, their types and uses,
as well as the faults and how to maintain them.

1
History

Node.js was created by Ryan Dahl in 2009 with the goal of developing a
scalable network application, leveraging the V8 JavaScript engine. In 2010,
it introduced a package manager, npm, which quickly gained popularity
within the developer community. The collaboration with Microsoft in 2011
led to the improvement and expansion of Node.js, making it more accessible
to a wider audience.

The architecture of Node.js sets it apart from alternative runtime


environments with its single-threaded processing and event loop system,
which allows for non-blocking I/O operations and asynchronous
programming. This makes it highly efficient for handling many concurrent
connections, making it well-suited for building real-time applications.

In 2015, the Node.js Foundation was formed, consolidating the governance


and overall evolution of the platform. Today, Node.js continues to evolve
and grow in popularity, becoming a fundamental tool for building modern
web applications.

Node.js

Node.js is a cross-platform runtime environment that allows developers to


execute JavaScript code outside a web browser. Its primary purpose is to
enable the creation of web services and to provide the ability to interact
with input/output devices, such as networking and file systems.

Key features of Node.js include its event-driven architecture, non-blocking


I/O, and support for asynchronous programming. It originated in 2009 as a

2
project led by Ryan Dahl, with the goal of extending JavaScript's reach
beyond the browser. Node.js is now commonly used for writing server-side
code for websites and desktop programs, thanks to its efficiency and
scalability.

In summary, Node.js is a powerful platform for executing JavaScript code on


various operating systems, making it an essential tool for creating web
services and interacting with input/output devices.

Node.js is a popular runtime environment that allows for the execution of


JavaScript code outside the web browser. One of its key features is its
asynchronous and event-driven nature, which means that it can handle
numerous concurrent requests without getting bogged down. This makes it
particularly well-suited for building scalable network applications.

Node.js is also known for its speed, thanks to its use of the V8 JavaScript
engine. Despite being single-threaded, it is highly scalable due to its ability
to offload tasks to the operating system, making it ideal for handling many
connections at once.

Another important aspect of Node.js is its lack of data buffering, which


means that it can quickly send and receive data without waiting for large
buffers to fill. furthermore, Node.js is released under the MIT license,
allowing for a wide range of usage and modification. Overall, Node.js is a
powerful and versatile platform with a range of features that make it well-
suited for building fast, scalable, and efficient network applications.

Advantages of NodeJS

Here are the advantages of using Nodejs for server-side programming:

3
1. Easy to learn:

Most developers have a good grasp of JavaScript for it being one of the most
popular programming languages.

2. Supported widely by Google Chrome:

The search engine is empowered by Node.js and it not only aids in the front-
end development but also has a great impact on the backend.

3. Node.js offers easy scalability:

One of the key benefits offered by Nodejs is that it makes it easy for
developers to scale the application in horizontal as well as vertical
directions.

4. Full-stack JavaScript:

Node.js has regarded as a full-stack JavaScript for serving server-side


applications as well as the client.

5. Offer High Performance:

As mentioned earlier, Node.js interprets the JavaScript code through


Google’s V8 JavaScript engine.

6. Single Programming Language:

With Node.js it becomes possible for developers to write the server-side


application in JavaScript.

7. Large and Active Community:

Node.js have a large and active community of developers who keep on

4
contributing to the further improvement and development of this server-
side JavaScript.

8. The ability of Caching:

The open-source runtime environment of Node.js provides the ability to


cache a single module. The entire request for the first modules gets cached
in the application memory.

9. Support for Commonly Used Tools:

With Node.js, developers receive extended support for various commonly


used tools.

10. Handles Requests Simultaneously:

Node.js provides the option of non-blocking I/O systems. It helps you to


process several server requests simultaneously.

Fig. 1 Advantages of NodeJS

5
Disadvantages of using Node.js

1. API is not stable:

One of the key problems that developers encounter while working on


Node.js is the Application Programming Interface keeps on changing at
frequent intervals and does not remain stable.

2. Library Support System is not strong:

Compared to other languages, JavaScript does not have a robust and

3. Asynchronous Programming Model:

If you want to boost the scalability of the application, the requirement is


that the application should adopt an asynchronous programming model.

4. Single-threaded nature:

Node.js is single-threaded, meaning it runs on a single thread and can only


utilize a single CPU core at a time.

5. Callback-based programming model:

Node.js relies heavily on callback functions to handle asynchronous


operations.

6. Memory consumption:

Node.js can consume more memory compared to some other server-side


technologies.

7. Limited support for multi-core scaling:

While Node.js is efficient in handling concurrent I/O-bound operations, it

6
lacks built-in support for scaling across multiple CPU cores.

8. Limited standard library:

Compared to some other programming languages, Node.js has a relatively


limited standard library.

Fig. 2 Disadvantages of NodeJS

Create a backend API in Express JS

To create a backend API in Express JS, we need to do the following steps:

1- Install Node JS and Express JS

Node JS is a runtime environment that allows you to run JavaScript code


outside of a web browser. Express JS is a web framework that provides a set
of features and tools for building web applications with Node JS.

To install Node JS, you can download it from the official website and follow
the instructions for your operating system. To install Express JS, you can
use the Node Package Manager (npm), which is a tool that comes with Node
JS and allows you to install and manage various packages and modules for
your project.

7
2- Create a project folder and a server file

To create a project folder, you can use any name you like, such as express-
api. To create a server file, you can use any name you like, such as app.js.
This file will contain the code for your backend API. To create the file, you
can use any text editor or IDE you prefer, such as Visual Studio Code,
Sublime Text, or Atom.

To start the server file, you need to import the express module and create
an instance of the express application. You also need to specify a port
number for your server to listen to.

3- Define the data model and the data array

To create a backend API, you need to have some data to work with. For this
example, we will use a simple data model for a blog post, which has the
following properties:

- id: A unique identifier for the post


- title: The title of the post
- content: The content of the post
- author: The name of the author of the post To store the data, we will use
a simple array of objects, which will act as a mock database.
4- Create the routes and the handlers for the API

To create the routes and the handlers for the API, you need to use the app
object that you created earlier. The app object has methods that correspond
to the HTTP methods, such as app.get, app.post, app.put, and app.delete.
These methods take two arguments: a path and a callback function. The
path is a string that defines the URL for the route, and the callback function
is a function that handles the request and sends the response.

8
5- Test the API with a tool like Postman

To test the API, you can use a tool like Postman, which is a software that
allows you to send and receive HTTP requests and responses. You can
download Postman from official website and follow the instructions for
your operating system.

What is a Server

A Server is a program or a device that provides functionality for called


clients which are other programs or devices. This architecture is called the
client-server model.

A single overall computation is distributed across multiple processes or


devices. Servers can provide various functionalities called services. These
services include sharing data or resources among multiple clients or
performing computations for a client. Multiple clients can be served by a
single server, and a single client can use multiple servers.

Uses of Servers

A client process may run on the same device. It can also connect over a
network to a server to run on a different device. Examples of servers may
include database servers, mail servers, print servers, file servers, web
servers, application servers, and game servers. Most frequently client-
server systems are implemented by the request-response communication.,
i.e., a client sends a request to the server. In this model, the server performs
some action and sends a response back to the client, typically with a result
or acknowledgement. designating a computer as server-class hardware
means that it is specialized for running servers on it. This implies that it is
more powerful and reliable than standard personal computers. However

9
large computing clusters may comprise many relatively simple, replaceable
server components.

Server Component

Together, server components are intended to offer clients functionality,


resources, and/or services. Specific components will differ based on the
form factor and function of a given server, but common components are
covered here.

- Hardware: The dedicated server’s central processing unit (CPU),


memory, storage device, network interfaces, and the server chassis are
all included in this.
- Server OS: This operating system (OS) was created with a particular
kind of client/server environment in mind.
- Network Connectivity: Over a local area network (LAN), wide area
network (WAN), or the internet, server programs connect to the
network architecture and communicate with client devices. To offer
redundancy and accommodate various network setups, certain server
form factors contain several network interfaces.
- Management and Monitoring Tools: Instruments for remote
management and performance monitoring are frequently included with
servers.
- Server Software: This server software supports a particular use case.
Software for email servers, web servers, and database servers are a few
examples.
- High-Availability Features: High-availability (HA) capabilities are
included on some servers to reduce downtime and guarantee continuous
operation. This involves having access to numerous storage systems,

10
backup power supply and network interfaces, and configuration
management tools that enable automatic failover and load balancing.

Server Works

A device needs to be set up to listen for client requests across a network


connection in order to perform the role of a server. The operating system
may include this functionality as an installed application, a role, or a
combination of the two.

The windows server operating system from microsoft has the capability to
hear and respond to client requests. The types of client requests the server
can handle increase with additional installed roles or services. Another
illustration is when an additional application called Apache is put on top of
an operating system to handle requests from web browsers. A client sends a
request over the network whenever it needs data or functionality from a
server. The server gets this request and provides the necessary information
in response. This is the client-server networking request and response
model, commonly referred to as the call and response model.

As part of a single request and response, a server frequently completes a


variety of additional tasks, such as confirming the requestor’s identity,
making sure the client has permission to access the data or resources
requested, and correctly formatting or returning the required response in
the expected manner.

Types of Servers and Their Applications

1. Application Server

These servers host web apps (computer programs that run inside a web

11
browser) allowing users in the network to run and use them preventing the
installation of a copy on their own computers. These servers need not be
part of the World Wide Web. Their clients are computers with a web
browser.

2. Catalog Server

These servers maintain an index or table of contents of information that can


be found across a large distributed network. Distributed networks may
include computers, users, files shared on file servers, and web apps.
Examples of catalog servers are directory servers and name servers. Their
clients are any computer program that needs to find something on the
network. An example can be a domain member attempting to log in, an
email client looking for an email address, or a user looking for a file

3. Communication Server

These servers maintain an environment needed for one communication


endpoint to find other endpoints and then communicate with them. These
servers may or may not include a directory of communication endpoints
and a presence detection service, depending on the openness and security
parameters of the network. Their clients are communication endpoints.

4. Computing Server

These servers share vast amounts of computing resources which include


CPU and random-access memory over a network. Any computer program
that needs more CPU power and RAM than a personal computer can
probably afford can use these types of servers. The client must be a
networked computer to implement the client–server model which is a
necessity.

12
5. Database Server

These servers maintain and share any form of database over a network. A
database is an organized collection of data with predefined properties that
may be displayed in a table. Clients of these servers are spreadsheets,
accounting software, asset management software, or virtually any computer
program that consumes well-organized data, especially in large volumes.

6. Fax Server

These servers share one or more fax machines over a network which
eliminates the hassle of physical access. Any fax sender or recipient is the
client of these servers.

7. File Server

Shares files and folders, storage space to hold files and folders, or both, over
a network. Networked computers are the intended clients, even though
local programs can be clients.

8. Game Server

These servers enable several computers or gaming devices to play


multiplayer games. Personal computers or gaming consoles are their
clients.

9. Mail Server

These servers make email communication possible in the same way as a


post office makes snail mail communication possible. Clients of these
servers are senders and recipients of email.

13
10. Print Server

These servers share one or more printers over a network which eliminates
the hassle of physical access. Their clients are computers in need of printing
something.

11. Proxy Server

This server acts as an intermediary between a client and a server accepting


incoming traffic from the client and sending it to the server. Reasons to use
a proxy server include content control and filtering, improving traffic
performance, preventing unauthorized network access, simply routing the
traffic over a large and complex network. Their clients are any networked
computer.

12. Web Server

These servers host web pages. A web server is responsible for making the
World Wide Web possible. Each website has one or more web servers.
Their clients are computers with a web browser.

Why Are Servers Always On?

The servers are always on because they are commonly used to deliver
services that are constantly required, most servers are never turned off.
Consequently, when servers fail, they can cause network users and
companies many problems. To alleviate these issues, servers are commonly
set up to be fault tolerant.

Any computer act as a server with the right software. You may install an
FTP server program on computer and share files between other computers
over your network. But, you can make your home computer a server. you

14
have to keep some important points in your mind:

- The computer and the related server software must be in a running


mode that is accessible at any time.
- A computer is in server mode, its resources (like bandwidth and
processing) will not allow it to do any other things.
- If the services become popular, which you are providing, a typical
computer can not be capable of handling all of the requests.

Server Maintenance Plan

A Server Maintenance Plan Generally Requires The Following:

- checking server log files


- assessing hard disk space
- examining folder permissions
- monitoring network temperature applications
- ensuring adequate redundancy of systems
- examining security features
- installing security software patches
- reading server logs for security alerts or evidence of computer hacking
attempts
- updating antivirus software on all computers on the network
- updating critical service packs and software updates
- performing regular comprehensive back-ups to ensure that vital data
can be retrieved from storage in the event of a system failure.

15
Server Maintenance Steps To Success

1. Verify your backups are working.


2. Check disk usage.
3. Monitor RAID Alarms.
4. Update Your OS.
5. Update your Control Panel.
6. Check application updates.
7. Check remote management tools.
8. Check for hardware errors.
9. Check server utilization.
10. Review user accounts.
11. Change passwords.
12. Check system security.

Common Server Problems & How To Fix Them

Servers are an essential part of any business, but they can be prone to a
variety of problems.

Knowing how to troubleshoot problems with servers is critical for keeping


your company up and running at optimal levels.

1. Power outages

Shutting down the server unexpectedly or instability with the power source
can result in data loss and downtime.

Power source failures can also cause servers to malfunction or completely


shut down due to a lack of power supply from the source itself or from a
faulty power cable connection between the source and the server itself.

16
To solve this issue you must provide an alternative power supply, such as
an uninterruptible power supply (UPS), to keep the energy stable under any
condition. in case of power loss, the UPS will supply your server so you can
have enough time to securely shut it down.

2. Dust and temperature interference

Dust and high temperatures can interfere with the performance of servers,
leading to slowdowns or even complete failure. Also, dust building up on
your servers will make them overheat faster, which can damage internal
parts and slow down their performance. Poor air conditioning in a server
room can lead to overheating which could result in hardware failure or
other issues that could affect the performance of your server(s).

To prevent it, make sure to use a clean room, with controlled air. This will
provide your server with both ideal temperature and minimum dust
contact. plus, an air-controlled room can also prevent moisture on your
server. Humidity can lead to a short-circuit, putting not only your data at
risk but your employees’ life as well.

3. Out-dated firmware

Failing to keep up with regular updates can lead to security vulnerabilities


and other issues that could affect the performance of your server.

Since not all software operating systems (OS) make automatic updates,
especially for older servers, you’ll have to set updates manually and help
your team to perform them. This will improve your security protocols and
prevent errors due to corrupted files or drives.

17
4. Cyber attacks and viruses

Cyber attacks and viruses can cause data loss, slowdowns, or even complete
shutdowns if not properly addressed in a timely manner. The best way to
protect your servers, and the business data, is by applying cybersecurity
protocols. You can either hire a cyber security service to provide your
business with high-standard protection, or you can build a security plan
internally.

5. High traffic problems

Overload is a problem with servers that could result in slow page loads or
even crashes. This affects performances and causes frustration on your
team. To avoid, or fix, this issue you must use a server configuration that
best fits your business requirements and goals.

6. Hardware failure

Hard drive failures are one of the most common causes of server problems,
as they can lead to data loss or system instability if not addressed quickly
enough.

HDDs are delicate devices and prone to physical damage. However, SSDs
are not flawless and can have logical and physical losses as well. Also,
hardware such as hard drives and cables requires updates as well. And
incorrect hardware configuration can lead to poor performance or even
complete failure of a server. Regular checkups of the software and
hardware can prevent several problems with servers. Make sure to keep
regular backups off-site and choose different types of data storage devices
for your backups.

18
One more prevention for data loss due to hardware failure is to build a
server recovery plan.

7. Motherboard damages

Motherboard failures can be difficult to diagnose and fix, as they often


require specialized knowledge and tools for repair or replacement. It’s easy
to mistake motherboard issues for other hardware issues.

Other common problems with servers, such as overheating and power


outages, can cause motherboard damage. Of course, every device has its
own lifespan, and sometimes the server problem can be that the device’s
lifetime ends. The best way to prevent this problem with the server is by
keeping the hardware and firmware updated and preventing the server
from overheating.

8. Slow performance

This could be caused by a lack of resources, outdated hardware, or too


many users accessing the server at once. To fix this issue, you should check
the server’s hardware specifications and upgrade if necessary. You may also
want to consider adding more resources or limiting the number of users
who can access the server at one time.

9. Security breaches

This could be caused by weak passwords or outdated security protocols on


your network. To prevent security breaches in the future, you should
implement strong password policies and regularly update your security
protocols as new threats emerge. Plus, using two-factor authentication can
help unauthorized access to the server.

19
As well as using a VPN helps protect against malicious actors trying to
access sensitive data stored on your servers.

10. Human errors

Deleting files, unplugging hardware incorrectly, or downloading files from


phishing emails are a few examples of how human errors can lead to
problems with servers.

Here, the best action is training employees on cybersecurity, instructing


them about being careful while deleting files and adding a firewall and
other software to block data transfer from and to outside the networking.

Conclusion

Building backend servers and APIs with Node.js and Express.js provides a
powerful and flexible foundation for modern web applications. By following
best practices in development, testing, and security, developers can create
robust and scalable systems to meet the demands of today's dynamic digital
landscape.

Maintaining and managing your server on a timely and consistent basis is


one of the most crucial requirements to ensure the smooth running of your
Web Hosting Server. One way to ensure this is to create an effective server
maintenance plan. A server maintenance plan helps enhance server
security, protects your server and website against malware and cyber-
attacks, prevents data losses, provides a reliable backup solution, and
ensures maximum website uptime for your customers. Hence, ultimately—
it helps improve customer satisfaction and experience.

Therefore, use the tips mentioned above, assess server components for

20
daily, weekly, and monthly maintenance and build an ideal server
maintenance plan. However, if you aren’t technically proficient with server
maintenance and management, you can opt for our Managed Hosting
services at Reseller Club.

On the flip side, if you own an in-house team of experts, and need a reliable
VPS Hosting solution, check out our KVM-powered SSD and HDD VPS plans
to get high performance, complete server management, and more at an
affordable price.

21
Reference

1. https://www.evernex.com/blog/common-server-issues-and-how-to-
troubleshoot-them/
2. https://blog.resellerclub.com/what-is-a-server-maintenance-plan-and-
how-do-i-implement-one/
3. https://dev.to/bilal1718/how-to-create-a-backend-api-in-express-js-
e0k
4. https://kirbtech.com/common-server-problems/
5. https://www.linkedin.com/pulse/story-behind-nodejs-how-changed-
web-development-mihir-b-9uljf?trk=public_post_main-feed-card_feed-
article-content
6. https://hyperskill.org/university/javascript/introduction-to-node
7. https://netcomp.com.au/blog/troubleshooting-hardware-failures/
8. https://www.salvagedata.com/common-server-issues/

22

You might also like