Exploring the concept of global objects within the expressjs framework

Currently, I am working on building a school management system. One issue I encountered is related to the creation of global objects in my backend when a teacher sends a post request. I am wondering whether multiple teachers accessing the server will result in the creation of multiple global objects or if they will all access and potentially overwrite the same object.

Answer №1

When you mention creating a global object inside a route, are you referring to creating a property on:

1. global object itself -

global.myGlobalObj = { some-var: 'some-val' }
or

2. process object -

process.myGlobalObj = { some-var: 'some-val' }
or

3. express-app - possibly like app.set('myGlobalObj', someOb)

or perhaps on another global object. This would allow every request-response cycle to access the same object.

However, it is not advisable for any system you plan to develop. The main reasons include:

  1. It is unstable. In the event of your application crashing, any request relying on the current value of your global object will not complete properly since that value was lost/reset with the crash.

  2. This approach does not follow the principles of REST architecture, which emphasizes statelessness and self-sufficiency in requests.

  3. Globals go against Object Oriented Programming and Functional Programming paradigms. Global variables can make your application more vulnerable, less clear, and difficult to test.

Possible Solutions

If variable access performance is not critical, consider storing the variable in your database with an index and retrieving it for each request. Alternatively, use an in-memory database like Redis.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can we best optimize the conclusion of an infinite scroll when content is depleted?

Our web application has a feature that automatically loads more content when the bottom of the page is reached. Here's how it works: $window .on('scroll', function () { var $this = $(this) ; if ($this.scrollTop() == $do ...

Transitioning create-react-app from JavaScript to Typescript

A few months ago, I began a React project using create-react-app and now I am interested in transitioning the project from JavaScript to TypeScript. I discovered that there is an option to create a React app with TypeScript by using the flag: --scripts-v ...

Utilize SCSS values within TypeScript by applying them on a class level

let changeClassDisplay = document.getElementsByClassName('sidebar'); for (var i = 0; i < changeClassDisplay.length; i += 1) { changeClassDisplay[i].style.display = 'block'; } I encountered an issue with this code whe ...

Step-by-step guide to installing gatsby-CLI on Windows without requiring admin permissions

Currently, I am facing an issue while trying to install the gatsby CLI using the following npm command: npm install --global gatsby-cli I suspect the problem might be due to lack of admin access. Does anyone have suggestions on how to resolve this error? ...

What is the process for enabling autoescape universally in twig.js?

In my current project, I am utilizing express.js along with twig.js 0.9.5. My goal is to have autoescape enabled for all variables in each template. Although I came across this page suggesting that it should be the default behavior starting from version ...

Using jQuery, is it possible to retrieve the product name and image dynamically?

I'm currently working on implementing an add to cart functionality using jQuery. When the add to cart button is clicked, the product name and image should be displayed. I can achieve this statically but I need help figuring out how to dynamically retr ...

Could someone please assist me in figuring out the issue with my current three.js code that is preventing it from

Recently, I decided to delve into learning three.js and followed the getting started code on the official documentation. However, I encountered a frustrating issue where the scene refused to render, leaving me completely perplexed. Here is my index.html: ...

ERROR: Module 're2' not found in './build/Release/re2' (npm)

After receiving suggestions from sonarQube, I am attempting to switch out my original regular expression with RE2. However, upon installation, the following error message appears: Error: Cannot locate module './build/Release/re2' Important note ...

Tips for applying a class to an element depending on data stored in Local Storage using JQuery

I am currently working on a TODO list project with functions to save and delete records already in place. However, I am facing challenges with the functions for marking items as important and done. One method I use is to retrieve saved items from Local St ...

The issue at hand is the lack of execution for the Mocha Chai `.end()`

I have encountered an issue while trying to write a Mocha chai test for a Nodejs API that was previously tested using Supertest. Strangely, the test always passes even when I intentionally specify wrong expected parameters. Below is the code snippet of th ...

Utilizing Express.js and Postman to insert an embedded document in MongoDB

I am using postman with form-data to add a "Tournament" document through express.js and mongoose. If I were to make a get request by id, the resulting document should appear as follows: { "game_id": "62c97fd41755a3ff2a530f06", "matches": [ ...

Protractor is displaying an error message of "unable to locate element testability" when attempting to access an element

I'm encountering an issue with Protractor while trying to access a variable that stores the return value of "elements.all". As someone who is new to Protractor, I wasn't sure how to select elements by a custom attribute. Thankfully, I received so ...

"Using the power of jQuery to efficiently bind events to elements through associative

I am attempting to link the same action to 3 checkboxes, with a different outcome for each: var checkboxes = { 'option1' : 'result1', 'option2' : 'result2', 'option3' : 'result3', }; ...

Get rid of the arrow that is displayed when using the `time` input type in HTML5

Recently, I encountered an issue with the default HTML5 code snippet. My custom background looked perfect except for a pesky black arrow appearing on the right side. In this image, you can see the problematic black arrow that needs to be removed. I attemp ...

Encoding a two-dimensional array into JSON format

In my PHP script, I am querying a database and formatting the results as JSON: $query = mysql_query($sql); $rows = mysql_num_rows($query); $data['course_num']=$rows; $data['course_data'] = array(); while ($fetch = mysql_fetch_assoc($q ...

Does Javacc have a capability to generate JavaScript code as an output?

Is there a parser generator available that can take a Javacc grammar file (.jj) and produce a JavaScript parser instead of Java? If not, what would be involved in converting the .jj file into a format that ANTLR can interpret (since it has the capability ...

Guide on serving css files using express js

I'm currently facing difficulties sending css files using express. In my project structure, I have a src folder which contains the app.js file for the express code, and another folder named "public". Inside the public folder, there is an experience.ht ...

Querying a document by its Id using only JSON in MongoDB

I am trying to query a document in Mongodb (2.6.1) using pure JSON without using ObjectIds. According to the mongodb extended json documentation, I expected the code db.collection.findOne({"_id": {"$oid": "51b6eab8cd794eb62bb3e131"}}) to work but it is th ...

Using dynamic template URLs in resolving with Angular's UI-Router can provide flexibility

Currently, I have implemented a parent directive for an entire view to interchange templates based on the outcome of a promise. .directive('myDirective', function(myService) { var rootDir = '/path/to/templates'; return { ...

Running PHP scripts from JavaScript

Currently working on a PHP project that involves a dropdown select button in a webpage. The goal is to call a JavaScript function whenever the value of the dropdown changes, and then pass this selected value to retrieve additional details from a MySQL da ...