Let’s get started with node.js as a server side frame work

Rasitharoshana
9 min readMay 15, 2022

Introduction

Node.js is a JavaScript runtime environment that is open source and cross-platform. It is a widely used tool for nearly any type of job!

Outside of the browser, Node.js operates the V8 JavaScript engine, which is at the heart of Google Chrome. This enables Node.js to be extremely fast. A Node.js application operates in a single process, rather than establishing a new thread for each request.

Node.js’ standard library includes a set of asynchronous I/O primitives that prevent JavaScript code from blocking, and libraries in Node.js are often created following non-blocking paradigms, making blocking behavior the exception rather than the rule.

Node.js has the unusual advantage of allowing millions of frontend developers who write JavaScript for the browser to write server-side code alongside client-side code without having to learn a completely new language.

This enables Node.js to handle thousands of concurrent connections with a single server without incurring the load of thread concurrency management, which might be a substantial cause of errors.

What Does Node.js Have to Offer?

— Node.js can generate dynamic page content. Node.js can open, read, write, — remove, and close server files.
—Node.js has the ability to capture form data.
— Node.js has the ability to add, remove, and edit data in your database.

What exactly is a Node.js file?
— Node.js files include tasks that will be run in response to specific events.
— A typical occurrence is someone attempting to reach a server port.
— Before having any effect, Node.js files must be launched on the server.
— The suffix “.js” is used for Node.js files.

Why Node.js? — Node.js uses asynchronous programming!

Opening a file on the server and returning the content to the client is a frequent activity for a web server.

Here’s how PHP or ASP responds to a file request:

— The job is saved to the computer’s file system.
— Waits for the file system to open and read it.
— The material is returned to the client.
— I’m prepared to tackle the following request.
— The following is how Node.js processes a file request:

The job is saved to the computer’s file system.
I’m prepared to tackle the following request.
The server returns the file’s content to the client when the file system has opened and read it.
Node.js skips the waiting and immediately moves on to the next request.

Node.js is a single-threaded, non-blocking, asynchronous programming language that is extremely memory efficient.

Download Node.js

Download Node.js from the official Node.js web site: https://nodejs.org

How to install Node.js

Node.js can be installed in different ways. This post highlights the most common and convenient ones.

Official packages for all the major platforms are available at https://nodejs.dev/download/.

One very convenient way to install Node.js is through a package manager. In this case, every operating system has its own.

Other package managers for MacOS, Linux, and Windows are listed in https://nodejs.dev/download/package-manager/

nvm is a popular way to run Node.js. It allows you to easily switch the Node.js version, and install new versions to try and easily rollback if something breaks.

It is also very useful to test your code with old Node.js versions.

See https://github.com/nvm-sh/nvm for more information about this option.

In any case, when Node.js is installed you’ll have access to the node executable program in the command line.

History of node js

The timing was a major influence in the growth of Node.js. Only a few years prior, “Online 2.0” apps (such as Flickr, Gmail, and others) had begun to establish JavaScript as a more serious language, demonstrating to the world what a contemporary web experience might be like.

As numerous browsers raced to provide consumers the greatest performance, JavaScript engines improved significantly. Major browser development teams worked hard to improve JavaScript support and identify ways to make JavaScript run quicker. The Node.js engine, V8 (commonly known as Chrome V8 because it is The Chromium Project’s open-source JavaScript engine), improved greatly as a result of the competition.

Node.js was developed in the right location at the right time, but it isn’t the sole reason for its current popularity. It presents several new ideas and methodologies for JavaScript server-side programming that have already proven useful to many developers.

Getting started with MongoDB

Database applications may make advantage of Node.js.
MongoDB is one of the most popular NoSQL databases.

MongoDB

You will need access to a Database server to explore with the code samples.
A free MongoDB database may be downloaded at https://www.mongodb.com.
Alternatively, you may get started right immediately with a MongoDB cloud service by visiting https://www.mongodb.com/cloud/atlas.

Installing the MongoDB Driver

Let’s try using Node.js to connect to a MongoDB database.
To download and install the official MongoDB driver, use the Command Terminal and type:

You’ve now downloaded and installed the MongoDB database driver.
This module may be used by Node.js to control MongoDB databases:

Query Examples for node.js with MongoDB

Creating a Database

To build a database in MongoDB, first construct a Mongo Client object, then supply a connection URL with the right IP address and the database name.

If the database does not already exist, MongoDB will create it and connect to it.

Creating a Collection

To create a collection in MongoDB, use the createCollection() method:

Important: A collection in MongoDB is not formed unless it has content!

Insert in Collection

The insertOne() function is used to insert a record, or document as it is known in MongoDB, into a collection.

In MongoDB, a document is equivalent to a record in MySQL.

The insertOne() method’s first parameter is an object holding the name(s) and value(s) of each field in the document to be inserted.

It also requires a callback method where you can work with any problems or the insertion result:

Delete Document

To delete a record, or document as it is called in MongoDB, we use the deleteOne() method.

The first parameter of the deleteOne() method is a query object defining which document to delete.

Note: If the query finds more than one document, only the first occurrence is deleted.

Getting Started with MySQL

Database applications may make advantage of Node.js.
MySQL is one of the most widely used databases.

You must have MySQL installed on your computer in order to play with the code samples.
A free MySQL database may be downloaded from https://www.mysql.com/downloads/.

Setup the MySQL Driver

You may use Node.js to access MySQL after it is installed on your machine.
A MySQL driver is required to access a MySQL database with Node.js. This tutorial will make use of the “MySQL” module, which can be acquired from NPM.
To download and install the “MySQL” module, use the Command Terminal and type:

You now have a MySQL database driver that you have downloaded and installed.
This module may be used by Node.js to control the MySQL database:

Create Connection

Start by creating a connection to the database. Use the username and password from your MySQL database.

Save the code above in a file called “demo_db_connection.js” and run the file:

Creating a Database

To create a database in MySQL, use the “CREATE DATABASE” statement:

Query Examples for node.js with MySQL

Creating a Table

To create a table in MySQL, use the “CREATE TABLE” statement.

Make sure you define the name of the database when you create the connection:

Update Table

You can update existing records in a table by using the “UPDATE” statement:

Notice the WHERE clause in the UPDATE syntax: The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

Delete Record

You can delete records from an existing table by using the “DELETE FROM” statement:

In the DELETE syntax, take note of the WHERE clause: The WHERE clause defines which record(s) to be destroyed. If the WHERE clause is omitted, all records will be destroyed!

JavaScript for Node.js

Arrow Expressions

A new feature called arrow expressions was introduced with the launch of ES6 (ECMAScript) in 2015. Developers may now omit sections of a function they don’t require using arrow expressions. This implies that your code will be easier to maintain and arrange.

Take a peek at the code that follows. There are two distinct functions defined. The first is anonymous (the function is unnamed), whereas the second is. We don’t utilize the function declaration when utilizing an arrow expression. To create an arrow expression, just type () =>. Between the parentheses (()), you can give parameters to an arrow expression.

Asynchronous Concepts
We utilize a combination of synchronous (blocking I/O) and asynchronous (non-blocking I/O) code in Node.js and JavaScript programming. Promises are a frequent example of asynchronous programming.

Promises
A Promise is a JavaScript object that represents the asynchronous operation’s final result. There are three possible outcomes for a Promise: pending (the result is unknown and the expression is awaiting a response), fulfilled (the promise has been successfully completed and a value has been returned), and rejected (the promise did not successfully complete, the result is an error object).

A new Promise is created in the code below, and it is handed a function with two arguments: a satisfied condition and a rejected condition. The returned value is then logged.

Async/Await

Developers may simply construct Promise-based programming using the async…await syntax. When the term async is used with a function definition, an async function is created that returns a Promise. The await keyword in async methods allows us to halt the event loop until a Promise resolves or rejects. We may also attach the resolved value of a Promise to a variable using the await keyword.

Take a peek at the code that follows. The async keyword is used in the code below to construct an asynchronous arrow expression. We create a new Promise in the function body that passes a function that is called after 5 seconds, then we wait for the Promise to resolve and save the value returned to finalResult.

--

--