Using NodeJS and ExpressJs for Backend Development.

manuel__dev

Emmanuel Dodoo

Posted on February 15, 2023

Using NodeJS and ExpressJs for Backend Development.

What is NodeJs

Node.js is a server-side JavaScript runtime environment that enables developers to build and run web applications using JavaScript on the server-side. It is built on top of the Chrome V8 JavaScript engine and provides an event-driven, non-blocking I/O model that allows it to handle a large number of concurrent requests efficiently. Node.js is particularly useful for building scalable, high-performance web applications, real-time applications, and APIs, and has a large and active community of developers who create open-source modules and libraries to extend its functionality.

What is ExpressJS

Express.js is a popular open-source web application framework for Node.js that provides a set of tools and features for building web applications and APIs. It is designed to be minimal and flexible, allowing developers to create web applications using their preferred libraries and tools.

Express.js provides a range of features, including middleware support for handling requests and responses, routing for handling different HTTP requests, and template engine support for generating dynamic HTML pages. It also has a large and active community of developers who create open-source modules and plugins that can be used to extend its functionality.

With Express.js, developers can build web applications quickly and easily, making it an ideal choice for prototyping and building small to medium-sized web applications. It is also highly customizable and can be used to build complex, large-scale web applications and APIs.

What is a Backend-API

A backend API (Application Programming Interface) is a set of rules, protocols, and tools that enables communication between a client-side application (such as a web or mobile app) and the server-side backend of an application. The API defines how the client-side application can interact with the backend, and what data it can request or send.

In other words, a backend API is responsible for handling requests from the client-side application, processing the data, and returning a response back to the client. This response can take various forms, such as data in a specific format (such as JSON or XML), an error message, or a success message.

Backend APIs are essential for building modern web and mobile applications as they allow the client-side application to access and interact with data and functionality on the server-side. For example, an e-commerce application might use a backend API to handle requests for product information, add items to a shopping cart, process payments, and update inventory levels.

Next : Tutorial on how to create a Simple Backend Server for a School Management System using Node.Js and Express.Js

NB: Make sure you have Nodejs installed on your machine before you can continue. Nodejs download

1 . Creating your new Project.

create a new folder called school-management system (this is going to be our working directory, and open this folder in vs code or any text editor of your choice.

2 . Initialising a new Project and Installing Packages.

To create a new Node.js project, you can open a terminal and navigate to your desired working directory using the "cd" (change directory) command. Once you are in the correct directory, you can initialize a new project using the
npm init command. This command will present you with a series of questions, similar to a wizard that you might encounter when installing software on a Windows operating system. These questions will allow you to specify details about your project such as the project name, version, description, entry point, and dependencies.

image for tut

After initializing the project, you can install your dependencies for the project. This project will be a simple backend server that will allow you to create new students and find a particular student by their ID. The technologies we are going to be using are Node.js, Express.js, and Mongoose ODM, so to install the packages for the project we are going to install the following packages into our project by using the command: npm install mongoose express dotenv after doing this your package.json file should be looking like this:

packagejson

The node_modules folder contains all the dependencies installed for a Node.js project.

3 . Folder Structure for project

You can create the folders for your projects now: In your working directory, you can create another folder called src in this src, this is where we are going to keep all our source folders and files.
In the src folder we are going to create multiple folders and our main server file called server.js:

  • models
  • controllers
  • routes
  • config

In the models folder, we are going to create a file called student.model.js. In the controllers' folder, we are going to create a file called student.controller.js. In the routes folder we are going to a create a file called student.js. In the config file, we are going to create a file called dbconnection.js.
After doing this, your folder structure should look like this:

folder structure

4 . Writing the code for the project

First lets create a new a MongoDB Atlas Database for our project. Head over to MongoDB atlas
sign in or create a new account. After, create a new project and choose the plan for your cloud database. You can follow this link to know how to create a mongoDB atlas database Connecting MongoDB Atlas in a NodeJS project using mongoose.

After getting your mongoDB atlas link you can place it in your
.env file which you will create in the root directory of your project.
example : MONGODB_URI=<atlas link>

  • Describing your Student model. we are now going to be describing a Model for our student using the following code:

model code

  • Writing code for our student controller. After our model we are going to write controllers for our Student, the code should look like this:

student controller

  • Writing code for our student route

We are now going to specify the HTTP methods for specific routes and assign controllers to them. After doing this, your code should look like this.

routes

  • Writing code for our database connection

we are now going to write a function to connect to our atlas database
It should look like this:

db code

  • Writing code for our server file

In our server.js file which is the entry point for our project, we are going to listen on a port , connecting to the database, and also use our routes as global middleware. Your server file should look like this after completing it:

serverjs

After doing all this, you can run the command node ./src/server.js or you can create a script for it in your package.json file

NB: If you get any errors talking about module and commonjs, add the type property to your package.json.
eg: "type" : "module"

You can find the source code for the tutorial on github. Repo Link

5 . Testing Endpoints using Postman or Thunderclient.

You can now check if your endpoints are working by using postman(Postman is an HTTP API development tool used for testing, documenting, and sharing APIs.) or thunder client a vs code extension for testing REST APIS.

You can use this video to guide you on how to make API request using Postman. Postman Video

💖 💪 🙅 🚩
manuel__dev
Emmanuel Dodoo

Posted on February 15, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related