Skip to main content

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");

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

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>
Or
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

Comments

Popular posts from this blog

Generate XML file in Cakephp

Steps to Generate XML file using CakePHP: Step-1 Enable to parse xml extension in config route.php file.     Router::parseExtensions('xml'); Step-2 Add Request Handler Component to the Controller    var $components = array(‘RequestHandler’); Step-3 Add controller Action For XML Generation in Post Controller     function generateXMLFile()     {         if ($this->RequestHandler->isXml()) { // check request type             $this->layout = 'empty'; // create an empty layout in app/views/layouts/empty.ctp              }        }  Add header code in empty layout <?php header('Content-type: text/xml');?> <?php echo $this->Xml->header(); ?> <?php echo $content_for_layout; ?> Step-4 Set up View To generate XML Create xml folder inside Posts vi...

Simple JavaScript Object and Class Examples

In JavaScript, most things are objects. An object is a collection of related data and/or functionality Namespace: Everything you create in JavaScript is by default global. In JavaScript namespace is Global object . How to create a namespace in JavaScript? Create one global object, and all variables, methods, and functions become properties of that object. Example:  // global namespace var MYAPP = MYAPP || {}; Explaination: Here we first checked whether MYAPP is already defined.If yes, then use the existing MYAPP global object, otherwise create an empty object called MYAPP How to create sub namespaces? // sub namespace MYAPP.event = {}; Class JavaScript is a prototype-based language and contains no class statement.JavaScript classes create simple objects and deal with inheritance. Example: var Person = function () {}; Explaination: Here we define a new class called Person with an empty constructor. Use the class keyword class Calculation {}; A class expr...

How to Add Next Previous links to The Event Calendar

Add Next/Previous links to The Event Calendar Wordpress Plugin Add code to your child theme’s functions.php file /**  * Allows visitors to page forward/backwards in any direction within month view */ if ( class_exists( 'Tribe__Events__Main' ) ) { class ContinualMonthViewPagination {     public function __construct() {         add_filter( 'tribe_events_the_next_month_link', array( $this, 'next_month' ) );         add_filter( 'tribe_events_the_previous_month_link', array( $this, 'previous_month' ) );     }     public function next_month() {         $url = tribe_get_next_month_link();         $text = tribe_get_next_month_text();         $date = Tribe__Events__Main::instance()->nextMonth( tribe_get_month_view_date() );         return '<a data-month="' . $date . '" href="' . $url . '" rel="next"...