Node.js is a runtime environment that allows us to execute JS code on the server side. i.e. anywhere outside the browser can be my machine or anywhere , built on Google’s v8.

All browser have different JS engines:

Chrome - V8 Firefox - SpiderMonkey Safari - JavaScriptCore

Bun is a similar JavaScript runtime like node.js. It uses zig as engine.

Package.json is important only when we deploy a package to npm registry, not when we run the app locally. NPM is a package manager used for installing and managing external libraries/dependencies in our codebase.

Internal packages vs External Packages:

Former is provided to you by Node.js out of the box e.g. fs, PATH, http. These come bundled in node.js Latter are deployed in npm registry and need to be installed using npm.

Version :

”chalk” : “^5.3.0”

The format is MAJOR.MINOR.PATCH:

MAJOR : Significant updates or breaking changes

MINOR : Addition of new features or improvements in a backward compatible manner.

PATCH : Backward compatible bug fixes that address issues without adding new features or cause breaking changes.

If I mention “5.1.0” in package.json, npm install will bring exactly 5.1.0 but if I state “^5.1.0”, it may bring 5.3.0, 5.5.1 but not 6.0.0 .

package.lock.json : It locks the current version so that every developer running the code uses the same version.

We can create a CLI to do a custom operation using the package “commander”. What makes it a CLI is the fact that it take some command and does something /gives some output.


How the web works : When we enter google.com in our browsers, the browser knows the request needs to be sent somewhere but it doesn't know the destination IP…so it asks a DNS server for the corresponding IP of google.com, that's how we get the destination/google server IP, now the browser makes the HTTP request to the destination IP and the server responds with html,css,js for the browser to render.

HTTP Protocol was introduced so that servers/ clients & servers can talk to each other i.e. communicate. This method of client-server communications is called the request-response model (HTTP protocol) i.e. how a client and server (or 2 systems) communicate with each other.

Other communication protocols are : webRTC, webSockets, GRPC

In devtools/network tab, we can see the various network calls/ requests that go out and their corresponding responses as well:

Preview/Response tells us what the HTTP server responded with.

Domain Name/IP : Th way to reach a server is through its domain name, which resolves to an underlying IP. (google.com). Similar to phone calls, I call a person named “sandy” but under the hood I am calling the number “92355355045”.

GeoDNS is where the domain name resolves to different IP addresses based on user’s location. If I am in BLR, I will get the response from the google server in India. Similarly, if I am in Europe, I will get the response from EU server.

If there are multiple processes running on same server (app.100xdevs.com & projects.100xDevs.com), both have same IP since running on same server, in this scenario, ports are used to distinguish these processes so that the request/traffic goes to correct process. “A” will run on port 3000, “B” will run on port 3300 , for e.g.., so that traffic directed towards “A” will go to “A” and not be directed to “B”. Ports are logical endpoints which means they do not exist physically.

image.png

So, a single server can run multiple processes and respond with different data for different requests e.g., a react app on 1 port, a next js app on another port, although both have same IP (since hosted on same server ) but are on different ports. This saves cost since I don’t need to rent multiple servers to run >1 processes. We can run multiple websites on 1 aws server.

We can also use nginx on default port 443 (for HTTPS) which forwards the request to the concerned process i.e. both app.100xdevs.com & projects.100xdevs.com will show the IP as x.x.x.x:443 . This process is called reverse proxy.

***METHODS:

GET : Read data from the server

POST : Create data on the server

PUT : Update data on the server

DELETE : Delete data from the server

The default/first request to access a website will always be a GET request.***

HTTP status codes are three-digit numbers returned by a server to indicate the outcome of a client’s request. They provide information about the status of the request and the server's response.

Status Codes :

200 series (Success)

300 series (Redirection)

400 series (Client Error)