Wednesday, 11 January 2017

How to learn NodeJs in 5 steps

NodeJs Entry level Tutorial

Node.js is an open-source, cross-platform JavaScript runtime environment for developing a diverse variety of tools and applications. It is a server-side platform built on Google Chrome's JavaScript Engine

Follow Basic steps to run your first example
Step1: Download Nodejs and install it.
Step2: After installation Check NodeJs working or not
a) Open Windows Command Prompt
b) Execute the Command node -v. It print a version number
Example: C:\Users\Sudhir>node -v
Output: v6.9.2
c) Execute the Command npm -v. It print NPM’s version number
Example: C:\Users\Sudhir>npm -v
Output: 3.10.9

Step3: Type npm init and press enter
Step4: It will ask for file name, version and description etc
Step5: It will create a package.json file
"name": "nodetest",
"version": "1.0.0",
"description": "Hello test",
"main": "index.js",
"scripts": {
"start":"node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
"author": "",
"license": "ISC"
Step6: Create index.js and write console.log("Hello Sudhir");
Step7: Now run npm start

Or you can Simple follow these steps
Verify installation: Executing a File
Step1: Create a js file named main.js on your machine and simple output some message
console.log("Hello, Sudhir!");

Step2: Now execute main.js file using Node.js interpreter
Code: node main.js

Create Node Js First Simple Application
Create server : Server which will listen to client's requests.
Use the require directive to load Node.js modules.
Code: var http = require("http");

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello Sudhir\n');

Code Explanation:
http.createServer(): Create a server instance
listen(8081): Bind it at port 8081

Creating a package.json
> npm init
To create a package.json run:
> npm init --yes
It will create package.json file and ouput
"name": "nodeJs",
"version": "1.0.0",
"description": "",
"main": "hello.js",
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"nodemon": "^1.11.0"
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"keywords": [],
"author": "",
"license": "ISC"

Code Explation:
name: project name
version: always 1.0.0
main: always index.js // main server js file
scripts: by default creates a empty test script

Node Package Manager (NPM):
1. Used for install Node.js packages
2. Perform version management and dependency management of Node.js packages

Installing Modules using NPM

Install dependencies:
Code: npm install <Module Name>
Code: npm install

Start the server:
Code: npm start

Example1: To Get the form data, you have to add another package called body-parser
Code: npm install body-parser --save

Example2: Restarts the server automatically whenever you save a file that the server uses Nodemon
Code: npm install nodemon --save-dev

Example3: Express is a framework for building web applications on top of Node.js.
Code: npm install express --save

You can use this module in main sever by
Code: var express = require('express');

Stop the current server by hitting CTRL + C in the command line.

Uninstalling a Module
Code: npm uninstall express


Post a Comment