Node js tutorial - What is node js - learn node js
Related nodejs article tags - node js - node js tutorial - node js examples
What is NodeJS :
- Node.js lets us use JavaScript language on the server… so it allows us to write JavaScript outside the browser!
- Node.js has all the power of JavaScript and offers us a whole new way of developing dynamic websites.

- Asynchronous Event Driven Server Side JavaScript
- Non-blocking I/O and File API
- Runs on Google JS V8 Engine
- Single Threaded
- Highly Scalable
- Node Apps are created using JavaScript
- Faster in performance


- 1.Multiple requests are sent to the Node.js server.
- 2.The event-driven nature of Node.js creates a loop of events for all requests and handles them under a single thread.
- 3.These events are then delegated to the appropriate non-blocking event handler.
- 4.The response received travels the reverse way to the requested end point.
- 5.Since the process happens under a single thread, the response is sent asynchronously without causing page refresh.
Related nodejs article tags - node js - node js tutorial - node js examples
Explain NodeJS :
- An open-source runtime environment that contains JavaScript code for execution in the server to aid in asynchronous request-response strategy.
- A library with variety of modules needed for network and web application development.
- Is developed by Ryan Dahl in 2009.
- Is part of V8 JavaScript, a Google's implementation for the Chrome browser, with "Just in Time" compilation services to achieve high speeds for executions.
- Uses a non-blocking, event-driven I/O environment to handle high-critical real-time applications running across distributed devices.
- Prevents its applications from data buffers, regardless of its extended service processing tasks
Related nodejs article tags - node js - node js tutorial - node js examples
History of NodeJS :

learn nodejs - nodejs tutorial - nodejs example programs
How webpage works :

How webpage works
how nodejs works :
- Node.js offers a server-side environment, which allows us to also use JavaScript language to generate web pages. Basically, it replaces server languages such as PHP, Java EE, etc.

learn nodejs - nodejs tutorial - What is nodejs - nodejs example programs
Blocking model vs. non-blocking model :

- Considering a program, which upload a file and then display it. Below is the code written in a blocking model:
- Upload the file
- Display the file
- Do something else
- The actions are carried out in top to bottom order.
- The program will upload a file to the Internet.
- The program will display the file to the user.
- Then, the program can do other things (i.e. carry out other actions).
- Lets see the same code in a non-blocking model:
- Upload a file
- As soon as it’s finished, display the file
- Do something else
- The program wont carry out the tasks in the order that they are written. It does this:
- The program launches the upload of the file to the internet.
- The program does other things (the program follows its course).
- As soon as the upload is finished, the program carries out the actions that we asked it to do: it displays the file.
On the whole, the running of the program can therefore be represented as in the next figure.

learn nodejs - nodejs tutorial - Non blocking mode of nodejs - nodejs examples - nodejs programs
This is exactly how Node.js works. As soon as "File uploaded" appears, a function known as a callback function is called and carries out actions (here, the callback function displays the file).
// // Identical result to the previous code
var callback = function (error, response, body) {
console.log ("Hey Wikitechy File uploaded!");
});
request (‘http://www.site.com/file.zip',callback);
console.log ("I do other things while I’m waiting… ");
Related nodejs article tags - node js - node js tutorial - node js examples
nodejs sample code :

learn nodejs - nodejs tutorial - nodejs sample code - nodejs examples - nodejs programs
- Line 1
- Loading http module
- It is required to work with HTTP request and response
- It is required to create HTTP Server
- Line 2
- Create server using createServer() function
- It takes a callback as parameter . It takes HTTP request and response as paramtere
- Callback got two input parameter .
- Both input parameters are readable and writeable stream
- Line 4
- Writing response header
- It takes a JSON object as optional second parameter
- second parameter contains informations like
- Content Length
- Content Type
- Connection
- Accept Type

- Line 5-6
- Writing data in response
- Data can be written in form of string or buffer
- ServerResponse.end indicates the communication has been finished
- Line 9
- createServer() method is finished with chained http.Server.listen() method
- It specifies port number on which request will be listen Optional host name
- Line 10
- Printing a message
Related nodejs article tags - node js - node js tutorial - node js examples
nodejs workflow :

learn nodejs - nodejs tutorial - nodejs workflow - nodejs examples - nodejs programs
- It works on single thread
- It handles all request asynchronously on same thread
- It does not create new thread for each request which is very unlikely like other web servers
- It does not wait to complete a request
node js - Asynchronous Vs Event-Driven :
- Asynchronous code execution -
- Not all code is executed in the same order it is written
- Node (and you) divide the code into small pieces and fire them in parallel
- Typically the code announces its state of execution (or completion) via events
- Event-Driven code execution -
- A code block gets into the queue of execution when an event happens ------------- It gets executed on its turn
- Anyone can raise (or listen to) an event ------------- System
- You have the choice (and ways) to attach a code block to an event ------------- Also known as event callbacks
------------- Some library (module)
------------- Your code
