Node js

Node js

·

5 min read

Are you interested in backend development? Then Node.js is by far one of the most efficient technologies that you can build your backend architecture with. Node.js is a powerful and popular server-side JavaScript runtime environment that allows you to build scalable and high-performance web applications.

In this blog post, we'll explore what Node.js is and why it's a great choice for backend development.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a browser. It was initially released in 2009 and has since become widely popular among developers for building fast, scalable, and high-performance applications.Node.js is built on the google chrome V8 engine. It allows developers to write server-side applications using JavaScript, which was traditionally only used for client-side development. Node.js is lightweight, fast, and scalable, making it a popular choice for building web applications.

One of the key features of Node.js is its event-driven, non-blocking I/O model. This means that Node.js can handle a large number of connections and requests simultaneously without blocking the event loop. As a result, Node.js applications can handle a massive number of requests in a short amount of time, making it a great choice for building high-traffic web applications.

Setting up Node.js

Before you can start developing with Node.js, you need to set up your development environment. To do this, you’ll need to install Node.js and a package manager such as npm or yarn. Now you might wonder what is npm or yarn, well npm and yarn are both package managers for JavaScript. They allow developers to easily install and manage dependencies (third-party libraries or modules) for their projects. Node.js can be downloaded from the official website (https://nodejs.org/en) and installation is straightforward. Once Node.js is installed, you can use npm or yarn to install any required packages and dependencies.

Running your Node.js application

Once you’ve written your Node.js code, you can run your application using the node command followed by the name of your application file. You can then navigate to your application in a web browser to test it. There are also a variety of testing frameworks and libraries available for Node.js that allow you to write automated tests for your application, But in today's blog, we'll create our node app on the code editor. I'm using visual studio codes.

Step 1:

First, open up your VS code terminal or command prompt and create a new folder for your Node.js application. In this example, we'll call it "myFirstNodeApp".

mkdir myFirstNodeApp

Step 2:

Next, navigate into the newly created folder using the command:

cd myFirstNodeApp

Step 3:

Now we need to initialize our Node.js project. We can do this by running the following command:

npm init

This command will prompt you to enter various information about your project, such as the name, version, description, entry point, author, and more. You can simply press "Enter" to skip these prompts and accept the default values, or you can fill them out as desired.

Once you've completed the initialization process, you should see a new file called "package.json" in your project directory. This file contains metadata about your project as well as a list of all the packages that your project depends on.

Step 4:

Now that our project is initialized, we can start installing packages using npm. For example, if we want to install the popular Express.js web framework, we can run the following command:

npm install express

This will download and install the latest version of Express.js and add it to our list of dependencies in the "package.json" file. The package will also be installed in the "node_modules" folder in your project directory.

Now there are a few other Node.js frameworks that are substitutes for Express, such as Nest.js, Hapi.js, Fastify, Restify, etc. However, in this blog, we'll be using Express as it's the most widely used and beginner-friendly framework. Express provides a simple and intuitive API that makes it easy to build web applications in Node.js.

Step 5:

Since the practice of programming often begins with a 'Hello, World!' example, let's create a simple Node.js application that displays 'Hello, World!' on a web page. To get started, create a new file called 'app.js' in the root directory of your project and add the following code:

const express = require('express')
const app = express()

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(3000, () => {
  console.log('Server started on port 3000')
})

In this code, we first import the Express.js module using the "require" function. We then create a new instance of the Express.js application and define a route for the root URL ("/"). When a user visits the root URL, our server responds with the message "Hello World!" using the res.send function.

Finally, we start the server by calling the app.listen function and passing it the port number we want to listen on (in this case, 3000). When the server is ready to receive requests, we log a message to the console.

That's it for this basic introduction to Node.js and Express.js! From here, you can start exploring the vast ecosystem of Node.js packages and tools, and use them to build powerful and scalable web applications.

Step 6:

To fully utilize your node app, it is important to have a server running. Fortunately, starting a local server is easy. Simply navigate to the root directory of your project in a terminal window and enter the command "node app.js". This will start a server on your localhost , enabling you to run your project locally on your machine. So now you can use your Node app to its fullest potential!

node app.js

You should see the message "Server started on port 3000" in your terminal.

Now if you open your web browser and go to http://localhost:3000, you should see the "Hello World!" message.

Your final output should look like this:

Congratulation you, have built your first-ever node application.

That's it for this basic introduction to Node.js and Express.js! From here, you can start exploring the vast ecosystem of Node.js packages and tools, and use them to build powerful and scalable web applications.