What is Node.js?
- Node.js is an open source server framework
- Node.js is free
- Node.js runs on various platforms (Windows,
- Linux, Unix, Mac OS X, etc.)
- Node.js uses JavaScript on the server
Why Want to use Node.js?
- A common task for a web server can be to open a file on the server and return the content to the client.
- Here is how PHP or ASP handles a file request:
- Sends the task to the computer's file system.
- Waits while the file system opens and reads the file.
- Returns the content to the client.
- Ready to handle the next request.
Here is how Node.js handles a file request:
- Sends the task to the computer's file system.
- Ready to handle the next request.
- When the file system has opened and read the file, the server returns the content to the client.
- Node.js eliminates the waiting, and simply continues with the next request.
- Node.js runs single-threaded, non-blocking, asynchronously programming, which is very memory efficient.
What Can Node.js Do?
- Node.js can generate dynamic page content
- Node.js can create, open, read, write, delete, and close files on the server
- Node.js can collect form data
- Node.js can add, delete, modify data in your database
What is a Node.js File?
- Node.js files contain tasks that will be executed on certain events
- A typical event is someone trying to access a port on the server
- Node.js files must be initiated on the server before having any effect
- Node.js files have extension ".js
Install Node.js via NVM
Step -1
- sudo apt-get update
- sudo apt-get install build-essential libssl-dev
Step -3
- sudo curl https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
If no curl
sudo apt-get install curl
https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
Step -4
source ~/.profile
Step -5
nvm --version
Step-6
nvm install 6.16.0
Step -7
nvm ls
Step-8
node --version
Step -9
npm --version
* The following diagram depicts some important parts of Node.js which we will discuss in detail in the subsequent chapters.
What Concepts are in Node.js?
5.Where to Use Node.js?
Following are the areas where Node.js is proving itself as a perfect technology partner.
I/O bound Applications
Data Streaming Applications
Data Intensive Real-time Applications (DIRT)
JSON APIs based Applications
Single Page Applications
Data Streaming Applications
Data Intensive Real-time Applications (DIRT)
JSON APIs based Applications
Single Page Applications
--Node.js modules--
1.What is a Module in Node.js?
* Consider modules to be the same as JavaScript libraries.
* A set of functions you want to include in your application.
--Include Modules--
* To include a module, use the
require()
function with the name of the module:
1.var http = require('http');
* Now your application has access to the HTTP module, and is able to create a server:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
--Create Your Own Modules--
* You can create your own modules, and easily include them in your applications.
* The following example creates a module that returns a date and time object:
exports.myDateTime = function () {
return Date();
};
Use the
Save the code above in a file called "mycreatemodule.js"
var http = require('http');
var dt = require('./mycraetemodule');
** Include Your Own Module **
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time are currently: " + dt.myDateTime());
res.end();
}).listen(8080);
* The following example creates a module that returns a date and time object:
exports.myDateTime = function () {
return Date();
};
Use the
exports
keyword to make properties and methods available outside the module file.Save the code above in a file called "mycreatemodule.js"
var http = require('http');
var dt = require('./mycraetemodule');
** Include Your Own Module **
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time are currently: " + dt.myDateTime());
res.end();
}).listen(8080);
0 comments:
Post a Comment