Authentication
Explain what a “Singleton” is (in Computer Science terms)
A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. It contains static variables that can accommodate unique and private instances of itself. It is used in scenarios when a user wants to restrict instantiation of a class to only one object. This is helpful usually when a single object is required to coordinate actions across a system.
Explain how the Singleton pattern can be used with Node modules, specifically with classes
Sometimes you need to make sure that you have one and only one instance of an object. This is where the singleton pattern can be useful. A singleton represents a single instance of an object. Only one can be created, no matter how many times the object is instantiated. If there’s already an instance, the singleton will create a new one. Let’s take a look at where creating multiple instances of one object might create problems within our application.
Singleton is object that can have only a single, unique instance, with a single point of access. Node.js module system provides simple way to implement Singleton using module.exports. Module will be cached when it is accessed using require() statement. So our module is merely a cached instance although it behaves like a Singleton.
If you were tasked with building a middleware system like Express uses, what approach might you take to construct/operate it?
In Express, you can set up middleware to be “global” middleware; meaning it will be called for every incoming request.
Each middleware has access to the HTTP request and response for each route (or path) it’s attached to. In fact, Express itself is compromised wholly of middleware functions. Additionally, middleware can either terminate the HTTP request or pass it on to another middleware function using next (more on that soon). This “chaining” of middleware allows you to compartmentalize your code and create reusable middleware.
You’ve built a couple of custom middlewares to far, but there are lots of packages already built to do the things you might normally want to do. In fact, you’ve used the simple routing middleware library by using the app.get() or app.post() middleware functions. There are thousands of middleware libraries for doing things like parsing incoming data, routing, and authorization.
Okta has an Express middleware for OIDC security that I’ll show you to demonstrate using third-party middleware libraries.
| Term | Def |
|---|---|
| Router Middleware | Middlewares are features added on top of his basic handler, in the form of a stack of functions that take this request into a pipeline doing stuff with it (logging, parsing body, security …ect).Router is one of those middleware, what it does actualy is to take the original request, and forward it to a sub handler according to the path example : “/home” for a GET request is mapped to function getHome that handle it and eventually send a response to the client on the behalf of the original handler. |
| Dynamic Module Loading | is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory. It is one of the 3 mechanisms by which a computer program can use some other software; the other two are static linking and dynamic linking. Unlike static linking and dynamic linking, dynamic loading allows a computer program to start up in the absence of these libraries, to discover available libraries, and to potentially gain additional functionality. |
| Singleton Pattern | is a software design pattern that restricts the instantiation of a class to one “single” instance. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton. |
| CRUD -> REST Method Matches | CRUD stands for Create, Read, Update, and Delete, which are four primitive database operations.In a REST environment, CRUD often corresponds to the HTTP methods POST, GET, PUT, and DELETE, respectively. These are the fundamental elements of a persistent storage system. |
| Mock Testing | is an approach to unit testing that lets you make assertions about how the code under test is interacting with other system modules. In mock testing, the dependencies are replaced with objects that simulate the behaviour of the real ones. The purpose of mocking is to isolate and focus on the code being tested and not on the behaviour or state of external dependencies. |