Skip to content

Node.JS Server

jwberck edited this page May 5, 2017 · 5 revisions

A server is a device that provides services to clients across a network. A server needs to be able to respond to requests from the client quickly. However, since there is only one thread, a bottleneck can occur. A thread is a series of commands that exist as one unit of work that can be managed. A request can be made by the client, such as an image upload, that takes a large amount of time. This thread freezes the server, preventing it from handling any other requests.

Node.JS Architecture

Node.js leverages an asynchronous architecture to prevent the server from suffering thread bottlenecks. An asynchronous architecture allows multiple threads to be run at the same time. Normally, this would incur a lot of thread checking overhead that programmers would have to deal with constantly. They would need to keep every thread in mind when changing variables or executing methods.

Node.js doesn’t need these because it uses a single-threaded callback system. When a client makes a request, it is routed to an event. This event is then placed in the event queue. The single threaded event loop checks to see if there are any events in the event queue. Since it contains the client’s event, that event is processed. Completely processing a standard event consists of two parts: the asynchronous call, and the callback. When the event loop processes an event, it hands off the asynchronous call to external threads. Once those threads finish processing the data, its returned to the callback of the event and placed back on the event queue. The event loop continues to process any events ahead of the client’s event in the queue. Once at the front of the queue, the event loop finishes processing the callback and sends the response back to the client.

Node.JS Event Loop (Pillai, 2016) Node.JS Event Loop

Middleware

Middleware functions are executed in between a request and its handler or between handler and the response. They have access to the request object, response object, and the next middleware function in the request-response cycle (“Using Middleware”, 2016). In Node.js, Express.js is the dominant middleware framework available. Middleware can parse cookies, log events, encrypt data, and much more.

Routing

Middleware is often used for routing. For servers, routing refers to the system of selecting paths for incoming HTTP requests and binding them to specific server actions.

An example of a common routing technique is the initial call to a server. In this call, the client sends a request to the server with the initial domain path. By anticipating this call with a route, the server responds to the request with the HTML, CSS, and JavaScript that represents the website.

Storage

Accessing a database is one of the most computationally expensive operations possible for a web application. The two main ways to store data are in a relational database, and a NoSQL database. The majority of the time, data in a system is naturally relational. This makes relational databases the obvious choice for most situations, as modeling relations in a NoSQL database that uses documents would be substantially more difficult to modify. However, a NoSQL database can be beneficial in certain situations.

MongoDB is a document based NoSQL database and the M in the MEAN stack. It stores documents instead of tables, making document focused software, such as google drive, easier to handle across multiple clients. This technology is relatively new compared to classical SQL databases, which makes it much riskier to base a software stack on.

Clone this wiki locally