You will learn about node js installation, (dependencies) package installation, socket.io library, app deployment to Heroku (Cloud Hosting Platform)

Contents

  1. NodeJs Installation
  2. App Deployment (Heroku)
    • Creating a git repo
    • Creating Heroku Account
    • Pushing to Heroku
  3. Creating Node.js Application
    • Creating Hello World App
    • Creating Pointer
  4. Socket.io
    • Introduction to web framework (Express.js)
    • Creating a Simple Chat Appication

Note : Do not forget to remove the ** asterisk from any given command lines below.

NodeJs Installation

Download NodeJs from its official site, if you don’t have node.js installed in your system.

After installation, write commands as explained below in Command Prompt step by step :

1.  node -v (Display Node Js Version and used to check node js install properly)

2. npm init ( To create package.json)

package.json after creation

Note : npm init –yes or -y ( Will generate package.json without any question / answer )

package.json after creation

Note : you can set/config package options separately
npm set init.author.email “wombat@npmjs.com” ( Set config option for the init command)
npm config set init.author.name “Your Name”
npm config set init.author.email Your Email
npm config set init.author.url Your Domain
npm config set init.license MIT

1.Install dependencies

Here, we will add 2 dependencies package for now. i.e. express.js & socket.io

Brief Note :

1.Express.js
Express.js is a Node.js framework. Node.js is a platform that allows JavaScript to be used outside the Web Browsers, for creating web and network applications. This means that you can create the server and server-side code for an application like most of the other web languages but using JavaScript.
https://expressjs.com/

2.Socket.io
Socket.IO is a JavaScript library for realtime web applications. It enables realtime, bi-directional communication between web clients and servers. It has two parts: a client-side library that runs in the browser, and a server-side library for Node.js. Both components have a nearly identical API.
https://socket.io/

To Install Dependency packages

** npm install express –save **

** npm install –save socket.io **

npm install express.js –save ( To add an express entry to your package.json’s dependencies )

npm install –save socketio.io ( To add an socket.io entry to your package.json’s dependencies )

Update package.json file (write)
npm install –save-dev ( To add an entry to your package.json’s devDependencies )

Note : we just added two dependencies and update package.json file.

Updated package.json file
All installed packages will be inside node_modules folder which is generated while installing packages

Additional npm Commands / check npm documentation:

npm update ( Updating local packages )
npm outdated
npm uninstall < Package Name > ( Uninstall or remove package from node_modules)
npm uninstall –save < Package Name > ( To remove it from the dependencies in package.json )
npm install -g < Package Name > ( To download packages globally )
npm update -g < Package Name > ( To update global packages )
npm outdated -g –depth=0 ( To find out which packages need to be updated )
npm uninstall -g < Package Name > ( Global packages can be uninstalled )
npm publish ( To publish the package )

App Deployment (Heroku)

What is Heroku?

To be more precise, Heroku is a cloud platform that lets you to build, deliver, monitor your apps. It lets you host and manage your apps. You can manage up to 5 apps for free with basic feature that will do the most of the work you want but you get more with the enterprise feature. To know more, visit https://www.heroku.com/what

Creating a git repo


The heroku toolbelt installs the heroku command and the git version control system. You need to create a git repo in order to be able to deploy your app to heroku (there is no ftp here). To do this, run this command:

**git init**

Then, we need to tell git not to include the node_modules folder to your repo. This folder can grow quite large and it simply does not belong in git. To ignore the folder, create a new empty text file named .gitignore with the following content:
node_modules/

Now you can commit your code to your fresh new repo! Write these commands:

**git add**

**git commit -m ‘Initial commit’**

There is a special file that Heroku expects to find in your repo, in order to be able to start the application. It is called a Procfile – it is already created for you in the zip archive, so you don’t have to do anything. But if you are curious, it contains the following command:

**node app.js**

We are now ready to push the application to heroku!

Creating Heroku Account

1.Create an account, if you don’t have one already.

2. Install the heroku toolbelt for your operating system. It will give you access to the heroku command from a terminal window.

3. Initialize an empty git repository (explained in Creating a git repo page.)

4. Push your code to heroku. This will deploy it and give you a URL which you can share with your friends. (explained in Pushing to Heroku page)

You can also read this getting started guide, followed by this guide about running node.js applications.

Pushing To Heroku

The following two commands are only done the first time you start using the heroku utility. First you need to login to heroku from the command line tool:

**heroku login**

Then, you need to add your ssh key, so you can push code without entering a password:

**heroku keys:add**

Next, you need to create a new heroku application from the code in this folder:

**heroku create**

And finally, we are ready to push code! Type this command:

**git push heroku master**

This line will send your application code to heroku, where their servers will process your package.json file and install all libraries that your app needs. Do this every time you need to upload a new version of the code (you must have made a commit beforehand). To open your very own web chat in your browser run this command:

**heroku open**

This will open it in a tab in your default browser.

Creating NodeJs Application

1.Creating Hello World App

Before creating an actual “Hello, World!” application using Node.js, let us see the components of a Node.js application. A Node.js application consists of the following three important components −

Import required modules − We use the require directive to load Node.js modules.

Create server − A server which will listen to client’s requests similar to Apache HTTP Server.

Read request and return response − The server created in an earlier step will read the HTTP request made by the client which can be a browser or a console and return the response.

Step – 1
Import Required Module
We use the require directive to load the http module and store the returned HTTP instance into an http variable as follows −

// Load the http module to create an http server.
var http = require("http");

Step – 2
Create Server
We use the created http instance and call http.createServer() method to create a server instance and then we bind it at port 8081 using the listen method associated with the server instance. Pass it a function with parameters request and response. Write the sample implementation to always return “Hello World”.

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

Step 3 – Testing Request & Response
Let’s put step 1 and 2 together in index.js file and start our HTTP server as shown below −

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

Output :

2.Creating Pointer

In the Image above as we can see, we have set index.js file as the main file that will be accessed first. We will be studying about router later.

So create index.js in your document root.

IOT (Internet of Things)
Custom redirect url or page for wordpress login and registration

Leave a Comment

Your email address will not be published. Required fields are marked *