TechAE Blogs - Explore now for new leading-edge technologies

TechAE Blogs - a global platform designed to promote the latest technologies like artificial intelligence, big data analytics, and blockchain.

Full width home advertisement

Post Page Advertisement [Top]

How To Create Your First Node JS Application


How To Create Your First Node JS Application

Usually, when a beginner starts to learn new languages, he faces issues in creating a new project as he is unaware of the project files directory and commands to follow. I faced a similar problem but after researching, I found a solution so to help others, I am writing this simple tutorial on how to create your first Node JS application in two minutes only.

What is Node JS?

Node.js is a server-side open-source and cross-platform JavaScript runtime environment. Node JS uses an asynchronous non-blocking I/O model, which means that a single thread can handle multiple connections simultaneously. For instance, if you run a process that needs I/O or data from the database, it will resume operations when the response is received rather than blocking the thread.

Node.js has a distinct advantage in that frontend developers who write JavaScript for the browser can now write server-side code in addition to client-side code without having to learn a completely new language.

This is why Node.js is mostly used in Real-time chat and video calling applications. We will create these projects in the future and learn how it is effective in the real world.

PREREQUISITES:

  • Node JS installed
  • VS Code
  • Express and Nodemon Install

Table of Contents

  • Downloading Node JS
  • Installing Express and Nodemon
  • Creating index.js
  • Executing node application

Downloading Node JS

For downloading Node JS, you can visit https://nodejs.org/en/download/ and download the version for your PC.


Download Node JS

After installing Node JS, you can check the node and npm versions by running these commands:

> node --version
> npm --version

I have installed node v16.4.1 and npm version 8.6.0.

Step 1: Installing Express and Nodemon

Running the below command, Express and nodemon will be installed.


> mkdir demo # Creating new directory
> cd demo
> npm i express nodemon

Now a question arises, why are we installing these specifically?

That's because Express provides a minimalist approach to creating a web server whereas nodemon helps by automatically restarting the node application when file changes in the directory are detected.

Step 2: Creating index.js

There are two types of modules in node js, by default, all js files are treated as CommonJS modules so "require" is used whereas when you want to implement ES modules, you can add type as 'module' in your package.json file then you can use the "import" statement.


{
  "name": "proj1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type":"module",
   // ...
}

Now, "get" will be used to manipulate requests and responses, and "listen" will allow you to open the server locally on http://localhost:3005. Below is the code for the "Hello World" program.


const express = require("express"); //cjs
import express from "express"; //mjs

const app = express();

app.get("/", (req, res) => {
  console.log("Hello World!");
});

app.listen(3005, () => {
  console.log("App listening on port 3005!");
});

Step 3: Executing node application

Lastly, we can install node modules and initiate our node project.


> npm init
> npm install # For installing node modules

Now, in your terminal, you can write the below code to start your node application.


> nodemon index.js


Now, you guys have a basic understanding of Node JS. You would have realized how to set up the Node JS application all by yourself.

If you wish to check out more articles on the market’s most trending technologies like Big Data, Python, and Computer Vision, then you can refer here.

I will be releasing an article on how to connect your MongoDB database with Node JS and more.

See you next time,
@TechAE

No comments:

Post a Comment

Thank you for submitting your comment! We appreciate your feedback and will review it as soon as possible. Please note that all comments are moderated and may take some time to appear on the site. We ask that you please keep your comments respectful and refrain from using offensive language or making personal attacks. Thank you for contributing to the conversation!

Bottom Ad [Post Page]