0% found this document useful (0 votes)
28 views5 pages

Experiment 5

This document discusses Node.js concepts including callbacks, modules, and creating a Node.js application using callbacks. It explains that Node.js handles requests asynchronously and non-blocking by sending tasks to the file system and ready to handle the next request, instead of waiting for the file system. Built-in and custom modules can be included using require(), and callbacks allow passing functions to be executed later as asynchronous tasks complete. An exercise asks students to create a Node.js application using callbacks.

Uploaded by

darshana patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Experiment 5

This document discusses Node.js concepts including callbacks, modules, and creating a Node.js application using callbacks. It explains that Node.js handles requests asynchronously and non-blocking by sending tasks to the file system and ready to handle the next request, instead of waiting for the file system. Built-in and custom modules can be included using require(), and callbacks allow passing functions to be executed later as asynchronous tasks complete. An exercise asks students to create a Node.js application using callbacks.

Uploaded by

darshana patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT NO: 5 DATE: / /

TITLE: Node JS Concept of Callbacks .

OBJECTIVES: On completion of this experiment students will be able to…


• know the concept of Node JS.

THEORY:

• Node.js is an open source server environment


• Node.js is free
• Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
• Node.js uses JavaScript on the server

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"
Node.js Modules

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.

Built-in Modules

Node.js has a set of built-in modules which you can use without any further installation.

Node.js has a set of built-in modules which you can use without any further installation.

Here is a list of the built-in modules of Node.js version 6.10.3:

Module Description

assert Provides a set of assertion tests

buffer To handle binary data

child_process To run a child process

cluster To split a single Node process into multiple processes

crypto To handle OpenSSL cryptographic functions

dgram Provides implementation of UDP datagram sockets

dns To do DNS lookups and name resolution functions

domain Deprecated. To handle unhandled errors

events To handle events


fs To handle the file system

http To make Node.js act as an HTTP server

https To make Node.js act as an HTTPS server.

net To create servers and clients

os Provides information about the operation system

path To handle file paths

punycode Deprecated. A character encoding scheme

querystring To handle URL query strings

readline To handle readable streams one line at the time

stream To handle streaming data

string_decoder To decode buffer objects into strings

timers To execute a function after a given number of milliseconds

tls To implement TLS and SSL protocols

tty Provides classes used by a text terminal

url To parse URL strings

util To access utility functions


v8 To access information about V8 (the JavaScript engine)

vm To compile JavaScript code in a virtual machine

zlib To compress or decompress files

How to Include Modules?

To include a module, use the require() function with the name of the module:

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:

Example

Create a module that returns the current date and time:

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 "myfirstmodule.js"

Include Your Own Module


Now you can include and use the module in any of your Node.js files.

App.js

var http = require('http');


var dt = require('./myfirstmodule');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time is currently: " + dt.myDateTime());
res.end();
}).listen(8080);

Notice that we use ./ to locate the module, that means that the module is located in the same folder
as the Node.js file.

Save the code above in a file called "demo_module.js", and initiate the file:

Initiate demo_module.js:

C:\Users\Your Name>node demo_module.js

If you have followed the same steps on your computer, you will see the same result as the example:
http://localhost:8080

EXERCISE:

1.Create Node.js Application using Callback.

Signature with date: ________________

You might also like