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

How To Create Shortcodes In WordPress

We can create own shortcode by using its predified hooks add_shortcode( 'hello-world', 'techsudhir_hello_world_shortcode' ); 1. Write the Shortcode Function Write a function with a unique name, which will execute the code you’d like the shortcode to trigger: function techsudhir_hello_world_shortcode() {    return 'Hello world!'; } Example: [hello-world] If we were to use this function normally, it would return Hello world! as a string 2. Shortcode function with parameters function techsudhir_hello_world_shortcode( $atts ) {    $a = shortcode_atts( array(       'name' => 'world'    ), $atts );    return 'Hello ' . $a['name'] . !'; } Example: [hello-world name="Sudhir"] You can also call shortcode function in PHP using do_shortcode function Example: do_shortcode('[hello-world]');

How to replace plain URLs with links

Here we will explain how to replace Urls with links from string Using PHP $string ='Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world.  CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg   CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be'; $string = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $string); Using Javascript <script> function linkify(inputText) {     var replacedText, replacePattern1, replacePattern2, replacePattern3;     //URLs starting with http://, https://, or ftp://     replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;     replacedText = inputT...