Image for post
Image for post

In programming, functions are foundational. They modularized logic and which creates reusability and cleaner code. Sometimes functions are able to carry out their logic with outside input known as arguments. Arguments are then used within a function’s body to compute a return value. Functions are able to utilize arguments through function defined local variables called parameters. JavaScript functions also allow many different ways to work with parameters and arguments, some of which are not too familiar.

When working with parameters and arguments, the simplest variation is a function that expects a single argument.

function greet(greeting) {
console.log(greeting);
}
greet("hello"); // =>…

Image for post
Image for post

In JavaScript, functions are objects which provide a modular way to define a set of procedures or operations that can be used many times within a program. By using functions, code is abstracted away reducing potential repetitive logic and helping to maintain legibility. But when working with functions, it is important to know a few things about them, like the fact that they have access to many values that affect how the function can be used. For example, a common value that functions have access to are parameters, which are used as local variables within the code block of the function. Parameters are used to store input data which are then used within the function’s code block. Another not so common value that functions have access to are same name variables used to store the function object itself. …


Image for post
Image for post

Working with data can take two forms, either some work is done directly to it resulting in the data being fundamentally changed or it can be copied, and then have some work done to the copy. Most, if not all of the time, working with data in an immutable way is the preferred way because it increases the efficiency of being able to follow the flow of data and the changes made to it. For example, the React library uses this concept of maintaining immutability when working with state. The state of a component, or the data that describes the component at any given time, is only updated in a manner that keeps the old state intact. So, in order to begin working with data immutably, it must first be copied. …


Image for post
Image for post

Usually when we think of a data structure being iterable, we think of arrays and strings. JavaScript provides many ways to iterate over an array or string, ranging from the standard for-loop to more modern variation like the ES6 for/of loop. However, because objects are not inheritently iterable, JavaScript has defined the for/in loop which is specifically for iterating over objects. The for/in loop iterates over each property name or key in the object, by which then you could access the value. …


Image for post
Image for post

Objects are a fundamental data structure to any and all programming languages. They go by many names such as hashes, dictionaries, even associative arrays but what they really are is an unordered collection of properties or values that are retrieved by a name or key. In JavaScript, objects are reference data types, dynamic, can maintain their own properties but also can inherit properties from other objects. This brings us to the main idea, in JavaScript, objects follow what is known as “prototypal inheritance” which means that objects inherit from the prototype object of their constructor, also known as a class.

The easiest way to create an object is using the object literal syntax which instantiates a new Object object. …


Image for post
Image for post

In the previous blog, Getting Started with WebSockets, I went over the initial configuration of the server set-up to change its behavior to allow bidirectional communication between it and the client. The configuration included the Socket.io library which handled the configuration of the server, priming the application to take advantage of websocket connections and real-time communication. Picking up from there, after the application is configured to maintain open and persistent connections between the server and client, we can begin to implement various communication between server and client, known as events. The first type of event is between the server and an individual client, for example when a client connects to the server by visiting a specific URL and sending a GET request to that endpoint. The server at this point can listen for that client connection and respond to the client but maintain that connection open as opposed to the HTTP protocol in which after the server sends its response, the connection to the client is closed. …


Image for post
Image for post

In An Introduction to WebSockets With Nodejs, I went over the idea behind the WebSocket protocol and how it differed from the more common HTTP protocol. The WebSocket protocol enables persistent connections between the server and the client, with each connection known as a websocket. Such a connection allows the server to send data to the client without receiving a request from the client. In contrast, the connections made between a client and server in the HTTP protocol are terminated at the end of the process, or server response. If the server needed to send data to the client, it could not and would need the client to make a specific request for that data. And for a server to initiate communication with the client, the server must be configured in a specific way. Luckily this configuration is handled by a library called Socket.io, which enables bidirectional communication between the client and the server. …


Image for post
Image for post

By and large, every developer is familiar with the HTTP protocol and that is because it is foundational to every website and application. When a user visits an URL or resource endpoint, they trigger a request to a server, asking for specific data only accessible at that endpoint. And so, once the server receives this request it sends back a response, which can be anything from a webpage to some data. This relationship where a client initiates communication via a request, and only then does the server engage in communication via a response is considered unidirectional. Again, communication is only initiated one way, via the client. This type of communication works fine when data is unchanging or at least, not changing frequently. If this is not the case, as in real-time data flows, then the HTTP protocol is not the best approach for communicating with the server and accessing data. …


Image for post
Image for post

Applications become richer when they include the functionality of storing and utilizing files such as image, music, document files, and so on. And luckily Node has access to the Multer library which handles file uploads. Multer is a middleware that intercepts the POST request of the file upload to the server and works with the file in various ways before allowing the server to send out its response to the client. …


Image for post
Image for post

At some point, your web application will need to reach out to a resource for data. In web development, making HTTP requests can be done on the client-side or, in the case of Node, on the server-side. Many cases are made for one over the other, most notably that making requests via the server offers some added security but nevertheless, there are many ways to make those requests within Node. Of those ways, the longstanding and well-established method has been using the “request” library. So we will take a look at how “request” is implemented.

To get started, we will first install the…

About

Uriel Rodriguez

Flatiron School alumni and Full Stack web developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store