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

A Guide to UTF-8 for PHP and MySQL

Data Encoding: A Guide to UTF-8 for PHP and MySQL As a MySQL or PHP developer, once you step beyond the comfortable confines of English-only character sets, you quickly find yourself entangled in the wonderfully wacky world of UTF-8. On a previous job, we began running into data encoding issues when displaying bios of artists from all over the world. It soon became apparent that there were problems with the stored data, as sometimes the data was correctly encoded and sometimes it was not. This led programmers to implement a hodge-podge of patches, sometimes with JavaScript, sometimes with HTML charset meta tags, sometimes with PHP, and soon. Soon, we ended up with a list of 600,000 artist bios with double- or triple encoded information, with data being stored in different ways depending on who programmed the feature or implemented the patch. A classical technical rat’s nest.Indeed, navigating through UTF-8 related data encoding issues can be a frustrating and hair-pul...

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]');

Integrating Kafka with Node.js

Integrating Kafka with Node.js Apache Kafka is a popular open-source distributed event streaming platform that uses publish & subscribe mechanism to stream the records(data). Kafka Terminologies Distributed system: Distributed system is a computing environment where various software components located on different machines (over multiple locations). All components coordinate together to get stuff done as one unit.   Kafka Broker: Brokers are cluster of multiple servers. Message of each topic are split among the various brokers. Brokers handle all requests from clients to write and read events. A Kafka cluster is simply a collection of one or more Kafka brokers. Topics: A topic is a stream of "related" messages. Its unique throughout application. Kafka producers write messages to topics. Producer: Producer publishes data on the topics. A producer sends a message to a broker and the broker receives and stores messages. Consumers: Consumers read data from topics. A consu...