Understanding HTTP Requests in Node

Shyam Hande
Updated on 13-May-2020 11:31:28

378 Views

App.jsconst http = require('http'); const server = http.createServer((req, res)=>{    console.log(req); }); server.listen(3000);Run it with command: node App.jsWe can see what is inside a request object by opening a browser and navigating to localhost:3000There is lots of information which will get printed on console window. We will see some important properties.Identifying the url from where request came, request method type and headers in request is important.Headers will give us information on host and browser type , accepted response by host etc. Request method can be of any http method type like GET, POST, PUT, DELETE etc.const http = require('http'); const ... Read More

Understanding Node Lifecycle and Event Loop in Node.js

Shyam Hande
Updated on 13-May-2020 11:26:49

2K+ Views

Simple http server in node.js will register an event loop which will keep on listening for http requests.Execution of file containing code for creation of server will work as shown below −node App.js => Start executing script => Code parsing, Register events and functions => event loop=> keeps on running as long as event are registered.This is the single threaded event driven approach of the node.js. For accessing and updating of values in database also works using the event driven approach. Even though it’s a single threaded, it can handle multiple requests at a time due to its speed of ... Read More

Creating a Node.js Server

Shyam Hande
Updated on 13-May-2020 11:17:53

600 Views

The mostly used core modules of Node.js are −http − used to launch a simple server, send requestshttps − used to launch a ssl secured http serverpath − used to handle path based on operating systemfs − It’s a file system handling moduleos − its used for os related operationsLets build a simple http server using Node.js −Create a javascript file App.js (name it as you like) in an editor like visual studio code .App.jsconst http = require(‘http’); function reqListener(req, res){    console.log(‘Hello’); } const server = http.createServer(reqListener); server.listen(3000);ExplainationWe used const keyword instead of var or let to import a ... Read More

Display Image Inside SVG Circle in HTML5

Anvi Jain
Updated on 13-May-2020 11:09:15

10K+ Views

To display an image inside SVG circle, use the element and set the clipping path. The element is used to define a clipping path. Image in SVG is set using the element.ExampleYou can try to run the following code to learn how to display an image inside SVG circle in HTML5Live Demo           HTML5 SVG Image                                                                                              

Use Database Name with Special Characters in MongoDB Console

AmitDiwan
Updated on 13-May-2020 10:25:43

444 Views

Yes, use getSiblingDB(). Let us add some documents to the database −> use customer_tracker-990; switched to db customer_tracker-990 > db.demo1.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4697ca7e81adc6a0b3954") } > db.demo1.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea46980a7e81adc6a0b3955") } > db.demo1.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea46984a7e81adc6a0b3956") }Display all documents from a collection with the help of find() method −> db.getSiblingDB("customer_tracker-990").demo1.find();This will produce the following output −{ "_id" : ObjectId("5ea4697ca7e81adc6a0b3954"), "Name" : "Chris" } { "_id" : ObjectId("5ea46980a7e81adc6a0b3955"), "Name" : "David" } { "_id" : ObjectId("5ea46984a7e81adc6a0b3956"), "Name" : "Bob" }Read More

Limit Number of Documents in a Collection in MongoDB

AmitDiwan
Updated on 13-May-2020 10:24:09

487 Views

To limit the number of documents in a collection, set capped − true. Set the size there itself. Let us create a collection with documents −> db.createCollection( "demo683", { capped: true, size: 5 ,max:4} ) { "ok" : 1 } > db.demo683.insertOne({Value:100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468afa7e81adc6a0b394e") } > db.demo683.insertOne({Value:500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b0a7e81adc6a0b394f") } > db.demo683.insertOne({Value:1000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b1a7e81adc6a0b3950") } > db.demo683.insertOne({Value:400}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b2a7e81adc6a0b3951") } > db.demo683.insertOne({Value:800}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b3a7e81adc6a0b3952") } ... Read More

Skip Documents in MongoDB

AmitDiwan
Updated on 13-May-2020 10:22:17

190 Views

Yes, you can skip some documents using skip() in MongoDB. Use limit() to display how many documents you want to display after skipping some. Let us create a collection with documents −> db.demo682.insertOne({FirstName:"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462a804263e90dac94402") } > db.demo682.insertOne({FirstName:"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462ac04263e90dac94403") } > db.demo682.insertOne({FirstName:"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462af04263e90dac94404") } > db.demo682.insertOne({FirstName:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462b304263e90dac94405") } > db.demo682.insertOne({FirstName:"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462ba04263e90dac94406") } > db.demo682.insertOne({FirstName:"Chris"}); {    "acknowledged" : true,    "insertedId" ... Read More

Get the Top Most Document from a MongoDB Collection

AmitDiwan
Updated on 13-May-2020 10:19:35

227 Views

To get the topmost document, use find() along with limit(). To fetch only a single document, consider using limit(1). Let us create a collection with documents −> db.demo681.insertOne({_id:101, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo681.insertOne({_id:102, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo681.insertOne({_id:103, Name:"David"}); { "acknowledged" : true, "insertedId" : 103 } > db.demo681.insertOne({_id:104, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 104 } > db.demo681.insertOne({_id:105, Name:"Sam"}); { "acknowledged" : true, "insertedId" : 105 }Display all documents from a collection with the help of find() method −> db.demo681.find();This will produce the following output −{ ... Read More

Set $gt Condition in MongoDB

AmitDiwan
Updated on 13-May-2020 10:18:33

243 Views

The $and performs a logical AND operation on an array of one or more expressions. Let us create a collection with documents −> db.demo680.insertOne({Values:40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4461b04263e90dac943fe") } > db.demo680.insertOne({Values:70}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4461e04263e90dac943ff") } > db.demo680.insertOne({Values:[80, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4462a04263e90dac94400") } > db.demo680.insertOne({Values:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4463304263e90dac94401") }Display all documents from a collection with the help of find() method −> db.demo680.find();This will produce the following output −{ "_id" : ObjectId("5ea4461b04263e90dac943fe"), "Values" : 40 } { "_id" : ... Read More

MongoDB Query to Add Up Values of a Specific Field in Documents

AmitDiwan
Updated on 13-May-2020 10:09:06

294 Views

Let us create a collection with documents −> db.demo677.insertOne({Value:10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea421f404263e90dac943f8") } > db.demo677.insertOne({Value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea421f704263e90dac943f9") } > db.demo677.insertOne({Value:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea421fa04263e90dac943fa") } > db.demo677.insertOne({Value:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea421fe04263e90dac943fb") }Display all documents from a collection with the help of find() method −> db.demo677.find();This will produce the following output −{ "_id" : ObjectId("5ea421f404263e90dac943f8"), "Value" : 10 } { "_id" : ObjectId("5ea421f704263e90dac943f9"), "Value" : 50 } { "_id" : ObjectId("5ea421fa04263e90dac943fa"), "Value" : 20 } { "_id" : ... Read More

Advertisements