Hands on Node JS
Hands on Node JS
What is Node.js?
Node.js is a server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js was developed by Ryan Dahl in 2009.
Node.js is an open source, cross-platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux.Node.js also provides a rich library of various JavaScript modules which simplifies the development of web applications using Node.js to a great extent.
Features of Node.js
- Asynchronous and Event Driven
- Single Threaded but Highly Scalable
- Very Fast
- No Buffering
Hello World ! in Node.js
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
Importing required modules:
We use the require directive to load the http module and store the returned HTTP instance into an http variable as follows;
main.js
var http = require("http");
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 listenmethod.
main.js
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
Testing Request & Response:
To start the http server,
$ node main.js
Then we can verify the server startup using the console message,
Server running at http://127.0.0.1:8081/
Open http://127.0.0.1:8081/ in any browser and observe that the phrase "Hello World" is displayed
NPM comes bundled with Node.js installables after v0.6.3 version.
To verify the npm version installed,
$ npm --version
If you are running an old version of NPM then to update it to the latest version,
$ sudo npm install npm -g
main.js
var http = require("http");
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 listenmethod.
main.js
http.createServer(function (request, response) {
// Send the HTTP header // HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
Testing Request & Response:
To start the http server,
$ node main.js
Then we can verify the server startup using the console message,
Server running at http://127.0.0.1:8081/
Open http://127.0.0.1:8081/ in any browser and observe that the phrase "Hello World" is displayed
Node Package Manager (NPM)
It is the default package manager for the JavaScript runtime environment Node.js.
NPM, short for Node Package Manager, is two things:
1. it is an online repository for the publishing of open-source Node.js projects
2. it is a command-line utility for interacting with said repository that aids in package
installation, version management, and dependency management.
To verify the npm version installed,
$ npm --version
If you are running an old version of NPM then to update it to the latest version,
$ sudo npm install npm -g
Installing Modules using npm
Installation of a particular module can be easily done with the following command
$ npm install <Module Name>
Ex:
$ npm install express
Package.json
package.json is present in the root directory of any Node application/module and is used to define the properties of a package.Atrributes of package.json
- name − name of the package
- version − version of the package
- description − description of the package
- homepage − homepage of the package
- author − author of the package
- contributors − name of the contributors to the package
- dependencies − list of dependencies.NPM automatically installs all the dependencies mentioned here in the node_module folder of the package.
- repository − repository type and URL of the package
- main − entry point of the package
- keywords − keywords
Uninstalling Modules
Use the following command to uninstall a Node.js module,
$ npm uninstall <module name>
Updating Modules
Update package.json and change the version of the dependency to be updated and run the following command,
$ npm update <module name>
Searching Modules
Search a package name using NPM,
$ npm search <module name>
Creating Modules
We can create a new module using,
$ npm init
Thanks for posting. Its an important topic to be read.
ReplyDeleteMern Stack Online Training