wt unit4 part 2
wt unit4 part 2
js
Node.js is an open-source, cross-platform JavaScript runtime environment
that allows developers to run JavaScript code outside of a web browser:
What it does
Node.js is used to create server-side web applications, such as traditional websites
and back-end API services. It's also used for data-intensive applications, real-time
web applications, and hardware programming
Node.js REPL
The term REPL stands for Read Eval Print and Loop. It specifies a computer environment like a
window console or a Unix/Linux shell where you can enter the commands and the system
responds with an output in an interactive mode.
REPL Environment
The Node.js or node come bundled with REPL environment. Each part of the REPL environment
has a specific work.
Read: It reads user's input; parse the input into JavaScript data-structure and stores in memory.
Loop: It loops the above command until user press ctrl-c twice.
You can execute various mathematical operations on REPL Node.js command prompt:
1. Example: >10+20-5
2. 25
1. Example2: >10+12 + (5*4)/7
const fs = require("fs");
const filedata = fs.readFileSync('inputfile1.txt');
console.log(filedata.toString());
Events Module
Node.js has a built-in module, called "Events", where you can create-, fire-,
and listen for- your own events.
To include the built-in Events module use the require() method. In addition,
all event properties and methods are an instance of an EventEmitter object.
To be able to access these properties and methods, create an EventEmitter
object:
In the example below we have created a function that will be executed when
a "scream" event is fired.
Example
var events = require('events');
var eventEmitter = new events.EventEmitter();
Express Framework
Node.js has a built-in web server. The createServer() method in its http
module launches an asynchronous http server. It is possible to develop a web
application with core Node.js features. However, all the low level
manipulations of HTTP request and responses have to be tediously handled.
The web application frameworks take care of these common tasks, allowing
the developer to concentrate on the business logic of the application. A web
framework such as Express.js is a set of utilities that facilitates rapid, robust
and scalable web applications.
Installing Express
The Express.js package is available on npm package repository. Let us install
express package locally in an application folder named ExpressApp.
Save the above code as index.js and run it from the command-line.
Uniform interface
Statelessness
Client-server
Cacheability
Layered system
Code on demand
Scalability
Simplicity
Modifiability
Reliability
Portability
Visibility
A REST Server provides access to resources and REST client accesses and
modifies the resources using HTTP protocol. Here each resource is identified
by URIs/ global IDs. REST uses various representation to represent a resource
like text, JSON, XML but JSON is the most popular one.
HTTP methods
Following four HTTP methods are commonly used in REST based architecture.
POST Method
The POST verb in the HTTP request indicates that a new resource is to be
created on the server. It corresponds to the CREATE operation in the CRUD
(CREATE, RETRIEVE, UPDATE and DELETE) term. To create a new resource,
you need certain data, it is included in the request as a data header.
GET Method
The purpose of the GET operation is to retrieve an existing resource on the
server and return its XML/JSON representation as the response. It
corresponds to the READ part in the CRUD term.
PUT Method
The client uses HTTP PUT method to update an existing resource,
corresponding to the UPDATE part in CRUD). The data required for update is
included in the request body.
DELETE Method
The DELETE method (as the name suggest) is used to delete one or more
resources on the server. On successful execution, an HTTP response code
200 (OK) is sent.
const{MongoClient}=require('mongodb');
const url = 'mongodb://0.0.0.0:27017';
const database = 'pj';
const Client = new MongoClient(url);
Output
const{MongoClient}=require('mongodb');
const url = 'mongodb://0.0.0.0:27017';
const database = 'pj1';
const Client = new MongoClient(url);
The $lookup stage lets you specify which collection you want to join with the
current collection, and which fields that should match.
products
[
{ _id: 154, name: 'Chocolate Heaven' },
{ _id: 155, name: 'Tasty Lemons' },
{ _id: 156, name: 'Vanilla Dreams' }
]
Example
Join the matching "products" document(s) to the "orders" collection:
Save the code above in a file called "demo_mongodb_join.js" and run the file:
Run "demo_mongodb_join.js"
[
{ "_id": 1, "product_id": 154, "status": 1, "orderdetails": [
{ "_id": 154, "name": "Chocolate Heaven" } ]
}
]
As you can see from the result above, the matching document from the
products collection is included in the orders collection as an array.
The sort() method takes one parameter, an object defining the sorting
order.
dbo.collection("customers").find().sort(mysort).toArray(function(
err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
Run example »
Save the code above in a file called "demo_sort.js" and run the file:
Run "demo_sort.js"
[
{ _id: 58fdbf5c0ef8a50b4cdd9a86, name: 'Amy', address: 'Apple
st 652'},
{ _id: 58fdbf5c0ef8a50b4cdd9a8e, name: 'Ben', address: 'Park
Lane 38'},
{ _id: 58fdbf5c0ef8a50b4cdd9a8a, name: 'Betty', address: 'Green
Grass 1'},
{ _id: 58fdbf5c0ef8a50b4cdd9a90, name: 'Chuck', address: 'Main
Road 989'},
{ _id: 58fdbf5c0ef8a50b4cdd9a87, name: 'Hannah', address:
'Mountain 21'},
{ _id: 58fdbf5c0ef8a50b4cdd9a84, name: 'John', address:
'Highway 71'},
{ _id: 58fdbf5c0ef8a50b4cdd9a88, name: 'Michael', address:
'Valley 345'},
{ _id: 58fdbf5c0ef8a50b4cdd9a85, name: 'Peter', address:
'Lowstreet 4'},
{ _id: 58fdbf5c0ef8a50b4cdd9a8b, name: 'Richard', address: 'Sky
st 331'},
{ _id: 58fdbf5c0ef8a50b4cdd9a89, name: 'Sandy', address: 'Ocean
blvd 2'},
{ _id: 58fdbf5c0ef8a50b4cdd9a8c, name: 'Susan', address: 'One
way 98'},
{ _id: 58fdbf5c0ef8a50b4cdd9a8d, name: 'Vicky', address:
'Yellow Garden 2'},
{ _id: 58fdbf5c0ef8a50b4cdd9a91, name: 'Viola', address:
'Sideway 1633'},
{ _id: 58fdbf5c0ef8a50b4cdd9a8f, name: 'William', address:
'Central st 954'}
]