Lets get to it.
I. The Node Core
Servers and Clients: At the most basic level, a server is just a computer that is performing services that are requested of it from the client. The client requests and the server responds. The Internet runs on this client-server model of computing whereby the browser is the client and the web server is the server.
- Better ways to organize our code into reusable pieces
- Ways to deal with files
- Ways to deal with databases
- The ability to communicate over the internet
- The ability to accept requests and send responses
- A way to deal with work that takes a long time
II. Modules, Exports & Require
Require: is a function that loads and executes a module from a separate file by taking the files path as a parameter. The return value of the module is set by setting
module.exports inside the module file. We assign a variable to the return value of the require function in order to use the specific module.
require in Node, however it has a different syntax,
III. Events and Event Emitter
Events: something that has happened in our app that we can listen to and respond to. Events / Event Emitters are a fundamental foundational concept that constitute the majority of core Node JS API. All objects that can emit events inherit their capabilities from the
Inheriting from the Event Emitter(util.inherits vs ES6 Class extends): Before ES6, we used
util.inherits as a way to implement class-like inheritance. ES6 comes with the concept of
- Inheriting using util.inherits
- Inheriting using ES6 Class extends
IV Asynchronous Code
Callback: A function passed to some other function, which we assume will be invoked at some point. The function ‘calls back’ invoking the function you give it when it is done doing its work.
Event Driven, Non-Blocking I/O: We’ve already seen that inside Node, V8 is synchronous and runs one set of code at a time. Also inside Node is the libUV library, which deals with events occurring in the operating system.
- From the diagram above we can see that libUV connects by requesting something(e.g. open a file) from the OS.
- After some time (while v8 is still running without blocking) the OS sends back an event to satisfy libUV’s request.
- The event is then placed inside the queue, which holds all the events that have been completed.
- Then the Event Loop is constantly checking if something has happened inside the queue.
- When the event loop finds an event that is completed, it process it and sends a callback.
- Then V8 will run the callback ones the currently running code is done because it is synchronous. However, the entire process is asynchronous.
Buffer: A temporary holding spot for data being moved from one place to another. It is intentionally limited in size. Generally this data is coming and moving through a Stream. A buffer ultimately holds raw binary data. As such it’s not very often that we will directly deal with buffers. Most times buffers are something that’s coming back from some other utility.
Stream: A sequence of data made available over time (instead of trying to get the whole data at once). The pieces of data eventually combine into a whole. For e.g. watching a movie stream over the internet instead of downloading the full movie.
Pipes: Connecting two streams by writing to one stream what is being read from another stream. In Node we pipe from a Readable Stream to a Writable Stream. Since the
pipe method returns the destination stream we can chain another pipe method call on the destination stream (now a readable stream) and stream into another writable stream.
V. HTTP Webserver
Protocol: A set of rules two sides agree on to use when communicating. Both the client and server are programmed to understand and use that particular set of rules.
TCP/IP: in the Client-Server model each computer is assigned a unique IP (Internet Protocol) address in order for the client and server identify each other. After identifying each other the client and server then open a socket connection between them. We send information across this socket connection. The information we are sending is split into small packets of data and sent across the socket from one computer to another. The act/way of splitting this information into small packets and sending them across the socket is called TCP ( Transmission Control Protocol). The individual packet of information may be defined by other protocols like HTTP, FTP, SMTP and others. The operating system has a TCP/IP capability and Node then provides us with the ability to access those features.
Port: This is how, once a computer receives a packet, knows what program to send it to. When a program is setup on the operating system to receive packets from a particular port, it is said that the program is ‘listening’ to that port. For e.g. a computer with an IP address of
126.96.36.199 and running a NodeJs program listening at port
443 will have a complete socket address of
188.8.131.52:443 . Obviously we usually assign a domain which maps to the socket address like
HyperText Transfer Protocol (HTTP): A set of rules (and a format) for a packet of data being transferred on the web via the TCP/IP protocols. For e.g.
HTTP Methods (Verbs): Specifies the type of action the specific request wishes to make inside the request itself. GET, POST, DELETE, and others.
Application Programming Interface (API): A set of tools available to us for building a software application. On the web the tools are usually made available via a set of URLs which accept and send only data via HTTP and TCP/IP. In other words, the URL instead of returning a web application it responds back with data. We can also send data to that URL as part of our HTTP request and the API will do something with that data.
Representational State Transfer(REST): An architectural style for building APIs. It means that when we handle HTTP requests we decide that the verbs and the URLs mean something. Having a RESTFul API means you design your API so that it responds to the HTTP request verbs in the way that it is expected. It standardizes the way of using the HTTP request so that when they’re sent they match what’s actually happening.
Express: is a minimalist web framework for Node.js
Middleware: code that sits between two layers of software. In the case of Express, sitting between the request and the response.
Static files: files that are not processed by code in a any way. HTML, CSS, and image files are ‘static’ files.They are just uploaded to the server and are just sitting there.