0% found this document useful (0 votes)
12 views

NODEjs lecture 5

Uploaded by

smeet l
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

NODEjs lecture 5

Uploaded by

smeet l
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

NODE JS

Introduction
Module:5
Contents

• Web Module
• Networking Module
• Node.js with mongodb (self study not for exams)
Web module
• What is a Web Server?
• A Web Server is a software application which handles HTTP
requests sent by the HTTP client, like web browsers, and
returns web pages in response to the clients. Web servers
usually deliver html documents along with images, style
sheets, and scripts.
• Most of the web servers support server-side scripts, using
scripting languages or redirecting the task to an application
server which retrieves data from a database and performs
complex logic and then sends a result to the HTTP client
through the Web server.
• Apache web server is one of the most commonly used web
servers. It is an open source project.
Web Application Architecture
• Client − This layer consists of web browsers, mobile browsers
or applications which can make HTTP requests to the web
server.
• Server − This layer has the Web server which can intercept the
requests made by the clients and pass them the response.
• Business − This layer contains the application server which is
utilized by the web server to do the required processing. This
layer interacts with the data layer via the database or some
external programs.
• Data − This layer contains the databases or any other source
of data.
• 1. Creating a Web Server using Node
• Node.js provides an http module which can be used to create
an HTTP client of a server. In demo is the bare minimum
structure of the HTTP server which listens at 8081 port.
• Refer server.js code in notes

• 2. Creating Web client using Node


• A web client can be created using http module. Let's check the
following example.
• Create a js file named client.js −
• Refer client.js in notes
Networking Module
• Node.js net module is used to create both servers and
clients. This module provides an asynchronous network
wrapper and it can be imported using the following
syntax.

• var net = require(“net”)


.net module
• net.createServer([options][, connectionListener]) -Creates a
new TCP server. The connectionListener argument is
automatically set as a listener for the 'connection' event.
• net.connect(options[, connectionListener])-A factory
method, which returns a new 'net.Socket' and connects to the
supplied address and port.
• net.createConnection(options[, connectionListener])-A
factory method, which returns a new 'net.Socket' and
connects to the supplied address and port.
• net.connect(port[, host][, connectListener])
• Creates a TCP connection to port on host. If host is omitted,
'localhost' will be assumed. The connectListener parameter
will be added as a listener for the 'connect' event. It is a
factory method which returns a new 'net.Socket'.
.net module
• net.createConnection(port[, host][, connectListener])-
Creates a TCP connection to port on host. If host is
omitted, 'localhost' will be assumed. The
connectListener parameter will be added as a listener
for the 'connect' event. It is a factory method which
returns a new 'net.Socket’.
• net.isIP(input)
• Tests if the input is an IP address. Returns 0 for invalid
strings, 4 for IP version 4 addresses, and 6 for IP version
6 addresses.
NODE JS with Mongo DB (SELF
STUDY PART of module)
• Node.js can be used in database applications. One of the most
popular NoSQL database is MongoDB.

• Install MongoDB Driver : To download and install the official


MongoDB driver, open the Command Terminal and execute
the following:

• Download and install mongodb package: C:\Users\Your


Name>npm install mongodb

• Node.js can use this module to manipulate MongoDB


databases:
• var mongo = require('mongodb');
Database create
• 1. To create a database in MongoDB, start by creating a
MongoClient object, then specify a connection URL with the
correct ip address and the name of the database you want to
create.
• MongoDB will create the database if it does not exist, and
make a connection to it.
• Refer demo_create_mongo_db.js in mongodb folder

• 2. Creating a Collection
• To create a collection in MongoDB, use
the createCollection() method:
• demo_mongodb_createcollection.js in mongodb folder
Insert
• Insert Into Collection
• To insert a record, or document as it is called in MongoDB, into
a collection, we use the insertOne() method.
• A document in MongoDB is the same as a record in MySQL
• The first parameter of the insertOne() method is an object
containing the name(s) and value(s) of each field in the
document you want to insert.

• Refer demo_mongodb_insert.js in mongodb folder


FIND
• In MongoDB we use the find and findOne methods to find data in a
collection.
• Just like the SELECT statement is used to find data in a table in a
MySQL database.
• Find One
• To select data from a collection in MongoDB, we can use
the findOne() method.
• The findOne() method returns the first occurrence in the selection.
• The first parameter of the findOne() method is a query object. In this
example we use an empty query object, which selects all documents
in a collection (but returns only the first document).

• Refer demo_mongodb_findone.js
Filter
• Filter the Result
• When finding documents in a collection, you can filter the
result by using a query object.
• The first argument of the find() method is a query object, and
is used to limit the search.
• Refer demo_mongodb_query.js
•Thanks

You might also like