Using Express.js 4 to manipulate data by reading from and writing to an array directly, without the need

I am currently developing an express.js sample application for my own reference. I need to store data without setting up a database at the moment.

I am curious about how I can save data to a file in express. It doesn't necessarily have to persist, but I aim to simulate a database with CRUD and RESTful routing.

Imagine I have this content in data.js

var entries = [
{"id":1, "title":"Hello World!", "body":"This is the body of my blog entry. Sooo exciting.", "published":"01/01/2017"}];


exports.getBlogEntries = function() {
    return entries;
}

exports.getBlogEntry = function(id) {
    for(var i=0; i < entries.length; i++) {
        if(entries[i].id == id) return entries[i];
    }
}

If we consider getBlogEntries as index, and getBlogEntry as show, how should I approach emulating create, update, and destroy? Will the data persist or will it be stored temporarily in memory and vanish upon refreshing the page?

Answer №1

In the world of Node.js, the default behavior is single-threaded which means you have the capability to create an object or array globally that will retain its data throughout the entire duration of the program's execution. This global object is also thread-safe for performing operations on it. Think of it like handling a static variable in C++.

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

Guide to making an object with a value sourced from the redux store

I am looking to develop an object with custom getters and a key representing language. Depending on the language key, different values should be returned by the getters. This is specifically for a customizable language selection feature. As an example, one ...

Are there any online tools available for generating SSJSON specifically designed for SpreadJS?

Currently utilizing Ubuntu, Ubuntu-Wine, and MS Office 7 on Wine. Interested in converting an xls template to ssjson for testing SpreadJS. Found some sjson file links on the Wijmo forum. Managed to successfully load it into SpreadJS, but unsure if the c ...

Combining a Python backend with an HTML/CSS/JS user interface for desktop applications: the perfect synergy?

Is it possible to seamlessly combine Python code with HTML/CSS/JS to develop desktop applications? For instance, I want to create a function in Python that displays "Hello World!" and design a visually appealing user interface using HTML/CSS/JS. How can I ...

"Assigning an array as a value to an object key when detecting duplicates in a

Let's assume the table I'm iterating through looks like this: https://i.stack.imgur.com/HAPrJ.png I want to convert each unique value in the second column into an object, where one of the keys will hold all values from corresponding rows. Here& ...

Moving an object in threeJS from one location to another, from point A to point

i stumbled upon some questions on SO, but I'm still struggling to figure out how to create a basic animation like the ones I saw. Essentially, all I want to do is move an object from point A to point B. I've tried using translate, but the object ...

byte sequence displays as binary data (angular + express)

I've been working on pulling files from a back-end Node.js / Express server and displaying them in an Angular front-end. Despite trying different methods, I keep facing the same issue - the data displayed at the front-end appears as a bytestring. Even ...

JavaScript For Each loops are really starting to frustrate me

My issue seems to be straightforward. I am attempting to update a list of objects called localData with another list of objects that I received after making an AJAX request (referred to as data). The process involves using two loops, however, when I atte ...

passing user ID between components with the help of Vue Router

I have a list of users retrieved from an API. I want to ensure that when you click on a user's name, it opens a separate page with their information and a unique link. UsersList Component <button @click="showUserPage(user.id)">Go to t ...

Organize routes into distinct modules in Angular 6

Currently grappling with routing in my Angular 6 application. Wondering if the structure I have in mind is feasible. Here's what it looks like: The App module contains the main routing with a parent route defining the layout: const routes: Routes = ...

waiting for a promise outside of an async function

I'm currently working on a nodejs project using express. I've come across a scenario in my app.js where I am using await without an async function, and oddly enough, it seems to work fine. My code checks if a file exists, and if not, it creates ...

In Electron, methods for detecting and resolving Error TS2304: Unable to locate the name 'unknown on(event: 'ready', listener: (launchInfo: unknown) => void): this are essential for smooth operation

Encountering an issue while running npm build. Electron version: 7.1.2 TypeScript version: ^2.5.1 Target version: ES2017 Here are the details of the error. When I performed the build under the following conditions: Electron version: 6.0.0 TypeScript ver ...

I am unable to update an array within my component using useEffect

Encountered two problems, firstly while attempting to pass an array of objects from a parent component to a child component, facing difficulties as shown below: function DropdownSome ({options, placeholder, someNum}) { const [value, setValue] = useState ...

Importing data from json into HTML/JavaScript

For a small project, I am working on adding product data to a JSON file and then loading that data into the JavaScript section of my HTML code. However, I keep encountering errors where Microsoft Visual Code indicates that the object I am trying to load is ...

Unable to render any charts with angular-chart.js

Utilizing the dependency angular-chart.js in my angular project has allowed me to showcase data visualizations on my admin page. Recently, I decided to upgrade angular-chart.js to version 1.1 and Chart.hs to version 2.5 based on the README.md guidelines o ...

Limit the y-axis on CanvasJS visualization

Is it possible to adjust the minimum and maximum values on the y-axis of the graph below: new CanvasJS.Chart(this.chartDivId, { zoomEnabled: true, //Interactive viewing: false for better performance animatio ...

What could be causing spacing problems with fontawesome stars in Vue/Nuxt applications?

Currently, I am working on implementing a star rating system in Nuxt.js using fontawesome icons. However, I have encountered an issue where there is a strange whitespace separating two different stars when they are placed next to each other. For instance, ...

Exploring the JavaScript Bitwise NOT Operator and the toString() Method

Greetings to all in advance! alert((~1).toString(2)); As a result, the output is: -10 However, in PHP/Java, it produces 11111111111111111111111111111110 I'm puzzled. What could be causing Javascript to include a "-" at the start of the output? ...

The homepage on Delphi is having trouble displaying the menus

My issue is not related to resolving a problem with a specific IDE, such as Delphi, but rather about identifying the root cause of the issue. As many of you may be aware, in Delphi 2010 (and possibly in earlier versions), there is a welcome page that displ ...

Loading a webpage once the loading bar has completed

Is there a way to modify the JavaScript code for my progress bar that loads from 1 to 100 percent so that it automatically redirects to another page when reaching 100%? Here is the current JavaScript code: var i = 0; function move() { if (i == 0) { ...

Implementing a clickable image using an anchor tag in JavaScript

I need to enhance my image tag in JQuery by adding an anchor tag alongside it. How can I accomplish this using JavaScript? Here is what I attempted: var imgg = document.createElement("img"); imgg.className='myclass'; $( ".myclass" ).add( "<a ...