Intoduction To Node JS
Intoduction To 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 Node.js handles a file request:
1. Sends the task to the computer's file system.
2. Ready to handle the next request.
3. 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, asynchronous programming, which is very memory
efficient
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"
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.
http.createServer(function(request, response){
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200,{'Content-Type':'text/plain'});
Create a Node.js file named "myfirst.js", and add the following code:
myfirst.js
var http = require('http');