Reading notes for Code Fellows!
Singleton refers to the singleton design pattern which is categorized as a creational design pattern. Singleton pattern is a design pattern that does nothing but define a class. The class is defined in such a manner that only one instance of the class will be created in the execution of the program.
Singletons are used where only a single instance os the class will be necessary to control action throughout the execution. They are commonly utilized for logging, driver objects, caching and thread pool, database connections.
In JavaScript, the main technical advantage for singletons is “lazy-loading”. The singleton will only be instantiated when first needed. The potential benefits of this will be use-case specific. A singleton might be a good choice when:
An example of a basic singleton implementation might be:
class Singleton {
constructor() {
if (!Singleton._instance) {
Singleton._instance = this;
}
return Singleton._instance;
}
}
Creating singletons in Node.JS is very simple when we utilize the Node.JS caching mechanism. modules are cached after the first time they are loaded, meaning that every call to require('something')
will get the exact same object returned, as if resolving the same file itself.
When we finish writing a class module, we don’t export the class, but rather we export an instance of the class instead. Node.JS caches and resuses this instance each time it is required. The code might look like this:
class Singleton {
constructor() {
this.message = 'I am an instance';
}
}
module.exports = new Singleton();
next()
to pass the request and response methods on to the next piece of middleware.module.exports
server.js
file require
the custom middleware.Examples of middlewares called setCurrentUser.js
, addNewHeader.js
and isLoggedIn.js
that can fetch a user through authentication steps, design a new header and validate a user’s data set:
Using the request
object
const getUserFromToken = require("../getUserFromToken");
module.exports = function setCurrentUser(request, response, next) {
const token = request.header('authorization');
const user = getUserFromToken(token)
.then(user => {
request.user = user;
next();
});
};
Applying the response
object
module.exports = function addNewHeader(request, response, next) {
response.setHeader('X-New-Policy', 'Success');
next();
};
Ending the request
and response
cycle
module.exports = function isLoggedIn(request, response, next) {
if (request.user) {
next();
} else {
response.send(401, "Unauthorized");
}
};
Vocabulary Term | Definition |
---|---|
Router Middleware | Router-level middleware works in the same way as application-level middleware, except it is bound to an instance of express.Router() “How Node JS middleware Works?” |
Dynamic Module Loading | With import() operator we can load modules dynamically. The import() works like a function, but it is an operator: In order to resolve “module specifiers” relatively to the current module, it needs to know from which module it is invoked. “Javascript tips — Dynamically importing ES modules with import() “ |
Singleton Pattern | A design pattern that does nothing but define a class. The class is defined in such a manner that only one instance of the class will be created in the execution of the program. GeeksforGeeks |
Mock Testing | 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. Devopedia |
CRUD | REST |
---|---|
CREATE | POST and PUT |
READ | GET |
UPDATE | PUT, PATCH and POST |
DELETE | DELETE |