Tips for handling numerous requests using Express.JS

I am currently working on an Angular 6 + Express.JS application and have encountered a problem. When multiple requests are made simultaneously, especially when there are more than 4 requests, all of them sometimes respond with a 404 error or get cancelled. I'm wondering if there is an issue with how I am handling requests in Express or if I need to make some adjustments for concurrent requests.

Here is the request handling code:

let requests = [];
files.forEach((file) => {
    if (file.type.toLowerCase().includes('zip')) {
        requests.push(this.imagesService.uploadArchive(file).pipe(first()));
    } else {
        requests.push(this.imagesService.saveImage(file).pipe(first()));
    }
});

forkJoin(requests).subscribe(
    (res) => res.forEach(response => {
        this.onSave.emit(response);
    }), 
    (error) => {
        console.error(error);
    }, 
    () => {
        this.close.emit();
    }
);

Express route handling:

router.post('/images',
    formidable({
        encoding: 'utf-8',
        uploadDir: path.resolve(__dirname, '..', '..', 'uploads'),
        multiples: true,
        keepExtensions: true
    }),
    (req, res, next) => {
        // Code goes here...
});

Responses:

UPDATE

router.post('/archives',
    formidable({
        encoding: 'utf-8',
        uploadDir: path.resolve(__dirname, '..', '..', 'uploads'),
        multiples: true,
        keepExtensions: true
    }),
    (req, res, next) => {
        // Code goes here...
    }
);

// Additional functions defined here...

UPDATE 2

While everything works fine if both user and server are on localhost (regardless of running with nginx or not), issues arise when the server is remote.

Answer №1

That piece of code did the trick.

async function uploadFiles(files: File[]) {
    of(files)
        .pipe(
            concatMap(files =>
                files.map(file => {
                    return this.imagesService
                        .saveImage(file)
                        .pipe(
                            map(),
                            catchError((error, caught) => {
                                console.error(error);

                                return empty();
                            })
                        );
                })
            ),
            concatAll(),
            toArray(),
            map(res => console.log)
        )
        .subscribe();
}

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

Having trouble making the jQuery post method work to invoke a function in a Coldfusion component

While attempting to invoke a cfc method through ajax using jQuery's post() method, I repeatedly encounter an error message stating that "the xxx parameter to the yyy function is required but was not passed in". Below is the cfc function in question: ...

What are some techniques for transforming text into JSON format?

Beginner inquiry: I'm struggling with manipulating text using JavaScript. Here is the text I want to transform: "5555 55:55: John: New York 6666 66:66: Jack: Los Angeles" My desired output after manipulation: [{ name:"John", address:"New York", n ...

The object literal can only define properties that are already known, and 'data' is not found in the type 'PromiseLike<T>'

When making a request to a server with my method, the data returned can vary in shape based on the URL. Previously, I would cast the expected interface into the returned object like this: const data = Promise.resolve(makeSignedRequest(requestParamete ...

Retrieve external document and offer for download

Is there a way to create a download link for a Google CDN file on the client-side? I have several links on my webpage that when clicked, should trigger a download. For example: <a href="//ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"&g ...

Obtaining a file using capybara independently of rails

Case Study: Attempting to access an external URL using Capybara for downloading a file. It is necessary to use Selenium or Webkit as the driver, since Rack-test does not allow visiting external URLs. This website utilizes iframes. The prompt for file dow ...

Retrieving text that has been HTML-escaped from a textarea using jQuery

Within my HTML file, I have a <textarea id="mytextarea"></textarea>. Suppose a user inputs the following text: <hello>, world! How can I retrieve res = "&lt;hello&gt;, world!"; based on the user's input? The current code s ...

Duplicate a form based on the selected option

My goal is to duplicate a form based on the numerical value selected from the drop-down menu. You can find the corresponding code in this JSFiddle. Here is the HTML code: <div class="travel_tour-info"> <h3>How many people are you booking for? ...

Utilizing models to establish the data type of an Observable within Angular

I have a simple query regarding a service. I have a method called getAllArticlesFromDb in my service that retrieves data from an API using an HTTP GET call. Here is the code for the method: article.service.ts getAllArticlesFromDb() : Observable<any> ...

Guide to creating an ES6 express application using webpack 2 and babel

In my .babelrc file, I have only included the es2015 preset and I am using a Webpack 2 configuration for my project. I want to create an express app in ES6. Here is the webpack configuration: const path = require('path'); module.exports = { t ...

Passing a selected value from the child to the parent component through state in React using hooks

I'm currently working on a Dropdown component that includes a select tag. When the user changes the value in the select, the state is updated to reflect the selected option. The StyledDropdown component is used for styling the select element. const D ...

Incorporating an HTML image into a div or table using jQuery

I am a beginner in using JQuery within Visual Studio 2013. My question is how to insert an img tag into a table or div using JQuery? For example, I have a div and I would like to generate an image dynamically using JQuery. Or, I have a dynamically create ...

Posting and deleting data in AngularJS using HTTP requests

Incorporating both angularjs and c# webapi in this situation. My approach involves calling the webapi from angularjs and passing a json array. Here is the Angularjs code snippet: factory.delete = function (json) { var url = 'myUrl'; ...

Tips for removing elements inserted with before() function

I have successfully implemented an alert-style div that is displayed when a user selects an option from a form and clicks the add to cart link. The jQuery .before() method is used to add the user's form selection to the div. I am facing two issues th ...

Align Image According to Dimensions of the Browser

I'm looking for a way to dynamically position an image based on the width and height of the browser within an HTML file. I know that adjusting an image's width/height is possible through code like this: <script> ...

What is the most efficient method for incorporating React into Wordpress while using Typescript?

Is it possible for me to utilize the React / React-Dom scripts that Wordpress has enqueued in my bundled javascript, while still being able to use the -dev installed React for development purposes? The javascript files are designed for a WordPress plugin ...

Maintaining ongoing subscriptions using rxjs in Angular 5

As a newcomer to rxjs in Angular 5, I find it a bit challenging to articulate my question, but I am hopeful for some guidance. I frequently encounter the same setup: Multiple Components to display identical data A single Service for accessing the data ...

Encountered an issue when trying to convert JSON into an array and loop through it in an HTML file using a

Currently, I am working on a project involving Angular where I am retrieving data from an API through a GET call. Specifically, one column's data is being converted from JSON to an array. However, when attempting to iterate over this array in the HTML ...

The NGX-Graph nodes mysteriously change color to deep black when the third property is applied

My goal is to create a tree structure using ngx-graph and have each node linked to a pop-up using "@material-extended/mde". However, I encountered an issue when trying to define the pop-up content based on each individual node by using the property "data". ...

Creating a dynamic Bootstrap carousel with a fixed hero header text inside a designated container - here's how!

I am currently working on a project to develop a responsive Bootstrap slider with a fixed hero header that remains consistent while the slider images change. The hero header needs to be aligned to the left within a responsive Bootstrap container and center ...

Create object properties as optional, declare them afterwards, and access them without needing to verify

I'm wondering if there's a way to work around type definitions. Let me provide an example to clarify my question. Suppose I want to define a type that consists of a large object containing multiple objects: type BigObject = { dom: HTMLElement, ...