Techbubbles

Explore what Node.js is?

 

What is Node.js?

Node.js is a runtime environment and library for running JavaScript applications outside the browser. If you are a JavaScript developer and you are used to doing front-end stuff then you can take that knowledge and use it in server side backend.Node.js is mostly used to run real-timer server applications and it shines performance using non-blocking I/O and asynchronous events. Node.js is open source and created in 2009 by Ryan Dahl. Node is third most popular project on GitHub.

When to use Node?

Node is great for streaming or event based real-time applications like Chat Applications, Game Servers, Ad Servers and Streaming Servers. Node is great when you want high levels of concurrency with little dedicated CPU time. You can install Node on Windows from here. make sure node executable has been added to your PATH system environment variable. To create Node js projects in Visual Studio 2015 get the Node.js tools for Visual Studio from here.

image

once the installation is complete open visual studio 2015 and create a new project select the Node.js project type under JavaScript, you have different options for selecting the template as shown in the following picture

image

The solution structure for console application looks as following

image

Lets say you want to make a basic HTTP Server that will then send Hello World plain text to our browser. You can do by writing the following few lines of code in app.js file

   1: var http = require('http');

   2:

   3: var server = http.createServer(function (request, response) {

   4:     response.writeHead(200, { "Content-Type": "text/plain" });

   5:     response.end("Hello World\n");

   6: });

   7:

   8: server.listen(7000);

for example if you want to read a file then you need a require module and the name of the module  is fs. This is synchronous file reading

   1: var fs = require('fs');

   2:

   3: var contents = fs.readFileSync('package.json').toString();

   4: console.log(contents);

   5:

   6: fs.readFile('package.json', function (err, buf) {

   7:     console.log(buf.toString());

   8: });

same piece of code can be written in asynchronous mode as below

   1: var fs = require('fs');

   2:

   3: fs.readdir('.', function (err, files) {

   4:     if (err) {

   5:         console.log('Error finding files: ' + err)

   6:     } else {

   7:         files.forEach(function (filename, fileIndex) {

   8:             fs.readFile(filename, function (err, buf) {

   9:                 if (err) {

  10:                     console.log('Error reading file:' + err);

  11:                 } else {

  12:                     console.log(buf.toString());

  13:                 }

  14:             });

  15:         });

  16:     }

  17: });

for example if you want to create a socket then you can write the following code

   1: var net = require('net');

   2:

   3: // The handler argument is automatically set as a listener for the 'connection' event

   4: var server = net.createServer(function (socket) {

   5:     console.log("Connection from " + socket.remoteAddress);

   6:     socket.end("Hello World\n");

   7: });

   8:

   9: server.listen(7000, "127.0.0.1");

Now a client can make a connection to your socket and get the information from server

   1: var net = require('net');

   2:

   3: var client = new net.Socket();

   4:

   5: client.connect(7000, "127.0.0.1");

   6:

   7: client.on('data', function (data) {

   8:     console.log('Data: ' + data);

   9:     client.destroy();

  10: });

  11:

  12: // Add a 'close' event handler for the client socket

  13: client.on('close', function () {

  14:     console.log('Connection closed');

  15: });

Basically in order to load a module in node js you use require function with the path of the file or directory containing the module that you would want to load. It then returns a variable which contains all exported functions. Official package manager for node is NPM. when you use npm command it installs all dependencies automatically with the environment. To install a package you can use

npm install – package_name

npm update

when you install a package using npm then it installs the packages locally in a folder nodes in your project.packages.json file contains all the dependencies that you are using in your application.The contents of packages.json looks as below

   1: {

   2:   "name": "_05_HelloWorldTCP",

   3:   "version": "0.0.0",

   4:   "description": "05_HelloWorldTCP",

   5:   "main": "server.js",

   6:   "author": {

   7:     "name": "test package",

   8:     "email": ""

   9:   }

  10: }

some of the popular Node modules are

image

Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript

image

Related Posts:

%d bloggers like this: