Getting Started with NodeJS (2024)

Introduction

In this article, I am going to introduce NodeJS with Node Package Module (NPM), step-by-step basic implementation and explanation.

This article covers the following areas of NodeJS.

  • Introduction of NodeJS
  • Installation of NodeJS and NPM
  • Node Package Module (NPM)
  • Package.json
  • Basic Example

NodeJS

NodeJS is an open-source, cross-platform runtime environment for developing server-side web applications. NodeJS also has an event-driven architecture capable of asynchronous I/O.

NodeJS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

Installation of NodeJS and NPM

Installation of NodeJS and NPM is straightforward using the installer package available at NodeJS official web site.

  • Download the installer from NodeJS WebSite.
  • Run the installer.
  • Follow the installer steps, agree the license agreement and click the next button.
  • Restart your system/machine.

Now, test NodeJS by printing its version using the following command in Command Prompt:

 > node -v 

and test npm by printing its version using command

 > npm -v 
 /*test.js file*/console.log("Node is working"); 

Run the test.js file using Node command > node test.js in command prompt.

Getting Started with NodeJS (1)

You are done with installation.

Node Package Module

NPM is the package module which helps javascript developers load dependencies effectively. To load dependencies we just have to run a command in command prompt:

 > npm install 

This command is finding a json file named as package.json in root directory to install all dependencies defined in the file.

Package.json

Package.json looks like:

 { "name": "ApplicationName", "version": "0.0.1", "description": "Application Description", "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/npm/npm.git" }, "dependencies": { "express": "~3.0.1", "sequelize": "latest", "q": "latest", "tedious": "latest", "angular": "latest", "angular-ui-router": "~0.2.11", "path": "latest", "dat-gui": "latest" }} 

The most important things in your package.json are name and version. Those are actually required, and your package won't install without them. The name and version together form an identifier that is assumed to be completely unique. Changes to the package should come along with changes to the version.

Repository

 { "repository": { "type": "git", "url": "https://github.com/npm/npm.git" }} 

Specify the place where your code lives. Through this repository, developers can reach out and contribute to your application. If the git repository is not GitHub, then the npm docs command will be able to find you.

Scripts

 { "scripts": { "start": "node server.js" }} 

NPM provide many useful Scripts like npm install, npm start, npm stop etc.

Some default script values are based on package contents.

 "start": "node server.js" 

If there is a server.js file in the root of your package, then npm will default the start command to node server.js.

Dependencies

 {"dependencies": { "express": "~3.0.1", "sequelize":"latest", "q":"latest", "tedious":"latest", "angular":"latest", "angular-ui-router": "~0.2.11", "path":"latest", "dat-gui":"latest" }} 

Dependencies are specified in a simple object that maps a package name to a version range. Version Name must be Version exactly.

If you want to install the latest version of a file, you just have to put latest in place of the version name.

Tilde(~) is used to tell "Approximately equivalent to version".

Basic Example

Create a server.js javascript file with following code

 /*server.js*/const http = require('http');const hostname = '127.0.0.1';const port = 3000;const server = http.createServer(function(req, res) { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n');});server.listen(port, hostname, function() { console.log('Server running at http://'+ hostname + ':' + port + '/');}); 

As we need http to create an http server we use require('http') and pass it to a variable named http

 var http = require('http'); 

We also need to defined hostname and port number, here we use localHost i.e 127.0.0.1 and port number 3000 and assign this to the variables hostname and port, respectively.

 var hostname = '127.0.0.1';var port = 3000; 

Next we create the http server using the createServer method.

 var server = http.createServer(function(req, res){ res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n');}); 

This created the server as well as a response having statusCode: 200, Content-Type header of plain text and and ends with the string Hello World. This is the response that the server can send to browser.

the function has two parameters req and res which is the request from and response to the server, respectively.

In our example we are creating responses.

We created the server, now we have to assign it a hostname and port number.

 server.listen(port, hostname, function() { console.log('Server running at http://'+ hostname + ':' + port + '/');}); 

Here, the server listens to localhost on port 3000 and prints "Server running at https://127.0.0.1:3000/" in command prompt.

Now Run server.js file un node using command

 > node server 

Open a browser and enter url https://127.0.0.1:3000/. The browser will display Hello World message on the screen.

Getting Started with NodeJS (2)

Getting Started with NodeJS (3)

Getting Started with NodeJS (2024)
Top Articles
The wealth building potential of dual income property - The Live Life Project
How to do a Reg A+ IPO on an OTC market - Colonial Stock
Fighter Torso Ornament Kit
Riverrun Rv Park Middletown Photos
Rabbits Foot Osrs
Craglist Oc
How to know if a financial advisor is good?
Miss Carramello
라이키 유출
Directions To Lubbock
Publix 147 Coral Way
2013 Chevy Cruze Coolant Hose Diagram
Mission Impossible 7 Showtimes Near Regal Bridgeport Village
Craigslist Pets Southern Md
2015 Honda Fit EX-L for sale - Seattle, WA - craigslist
Colorado mayor, police respond to Trump's claims that Venezuelan gang is 'taking over'
Puretalkusa.com/Amac
"Une héroïne" : les funérailles de Rebecca Cheptegei, athlète olympique immolée par son compagnon | TF1 INFO
Dtab Customs
Band Of Loyalty 5E
Schedule 360 Albertsons
Jang Urdu Today
How to Watch the Fifty Shades Trilogy and Rom-Coms
Contracts for May 28, 2020
Panolian Batesville Ms Obituaries 2022
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
What Time Does Walmart Auto Center Open
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Truvy Back Office Login
San Jac Email Log In
Gt7 Roadster Shop Rampage Engine Swap
Marlene2295
Darknet Opsec Bible 2022
Why Are The French So Google Feud Answers
Kids and Adult Dinosaur Costume
Craigslist Free Puppy
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Usf Football Wiki
Duff Tuff
Wattengel Funeral Home Meadow Drive
The Holdovers Showtimes Near Regal Huebner Oaks
Busted Newspaper Mcpherson Kansas
Bekkenpijn: oorzaken en symptomen van pijn in het bekken
'The Nun II' Ending Explained: Does the Immortal Valak Die This Time?
La Qua Brothers Funeral Home
Value Village Silver Spring Photos
Unblocked Games 6X Snow Rider
City Of Irving Tx Jail In-Custody List
Take Me To The Closest Ups
25100 N 104Th Way
Pilot Travel Center Portersville Photos
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 6267

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.