Sending file streams from the frontend to the backend service using Express.js and the Fetch

In my ExpressJS application, I have created an endpoint that needs to proxy a large raw binary to a remote backend REST service without storing anything in memory.

To achieve this, I initially looked into using the request module with code like:

req.pipe(request({method: "POST"}))

However, since the request library is deprecated, I switched to using fetch. Here's what I have so far:

app.post("/my-endpoint", async (req, res) => {
    try {
        const url = http://link-to-backend.com/

        const response = await fetch(url, {
            method: "POST",
            body: req.body
        });
        response.body.pipe(res);
    } catch (e) {
        res.status(500).send("error");
    }
});

The above code functions correctly, but I'm unsure if it saves the req.body data into memory before sending it to the backend API. I want to validate this and understand how to effectively ensure it doesn't.

Answer №1

These specific middlewares are designed to process request and response data in a more efficient manner by streaming them chunk by chunk, rather than storing the entire contents in memory:

app.post("/my-custom-endpoint", async (req, res, next) => {
  delete req.headers.origin; // consider deleting other unnecessary headers
  req.pipe(http.request(apiUrl, {
    method: "POST",
    headers: req.headers
  }).on("response", function(response) {
    res.writeHead(response.statusCode, response.statusMessage, response.headers);
    response.pipe(res);
  }).on("error", next));
});

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

The use of a Bootstrap row is leading to incorrect dimensions for FullPageJS

Within the body tag, I have included the following code snippet: <div id="container"> <div class="section profile"> <div class="row"> <div class="col-sm-6"> A </div> ...

Bespoke Socket.io NodeJS chamber

I am currently developing an application involving sockets where the requirement is to broadcast information only to individuals within a specific room. Below is a snippet of the code from my server.ts file: // Dependencies import express from 'expre ...

Locating Elements in Protractor: Exploring Nested Elements within an Element that is Also a Parent Element Elsewhere on the Page

<div class="base-view app-loaded" data-ng-class="cssClass.appState"> <div class="ng-scope" data-ng-view=""> <div class="ng-scope" data-ng-include="'partial/navigation/navigation.tpl.html'"> <div class="feedback-ball feedback- ...

What is the best way to use toggleClass on a specific element that has been extended

I have been experimenting with this code snippet for a while. The idea is that when I hover my mouse over the black box, a red box should appear. However, it doesn't seem to be working as expected. Could someone please review this script and let me k ...

Conditionally render a div in React with Next.js depending on the value of a prop

Struggling with an issue in my app and seeking some guidance. The problem arises when dealing with data from contentful that has been passed as props to a component. Specifically, I only want to render a particular piece of data if it actually contains a v ...

How can I resolve the issue of the mouse x/y glitch while drawing on an HTML canvas using JavaScript/jQuery?

I've been searching all over for a solution to this issue, but I just can't seem to find out what's causing the problem... This is my first attempt at working on something like this in my spare time. I hope to apply what I learn to create a ...

Transforming this JavaScript code to be less intrusive by implementing an event listener for an unidentified ID

As I work on enhancing the functionality of this user interface, I've encountered a challenge with two tabs that require a proper event listener to ensure my JavaScript functions smoothly. This obstacle has been hindering my progress, but as I delve i ...

Effective ways to resolve the ajax problem of not appearing in the console

I am facing an issue with my simple ajax call in a Java spring boot application. The call is made to a controller method and the returned value should be displayed in the front-end console. However, after running the code, it shows a status of 400 but noth ...

What is the method for adding line breaks to a JSON file?

I've been developing a Discord bot and I'm currently storing currency values in a json file. The functionality is working smoothly, but the issue I'm facing is that it's adding them to the json file in a single line which makes it diffi ...

Angular setPristine function is not functioning properly

I am looking to achieve a simple task - cleaning the $scope.user fields without encountering errors. if ($scope.contactForm.$valid) { $scope.user = {}; $scope.contactForm.$setPristine(); } However, I'm still experiencing v ...

Tips for including an external babel JS (ES6) file in an HTML document:

To include the babel js file in an HTML file, take a look at the code snippet below: <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudfl ...

The sh.exe is still unable to find the 'express' command even though the PATH variables have been correctly set

Currently running Windows 7 Ultimate 64-Bit, I've gone through the process of reinstalling node.js multiple times. Please refrain from suggesting a reboot, as I have already tried that countless times. The instructions I followed for installation are ...

The combination of MUI CardContent and flexBox seems to have issues when used in conjunction with typography and chips

Take a look at this React code using MUI. It's designed to create a Card that showcases Character Information. const CharacterPreview = ({ characterKey }: CharacterPreviewProps) => { return ( <Card sx={{ maxWidth: 256, borderRadius: 4 }}&g ...

Update the button text dynamically when clicked without using an identifier or a class

If we take a look at my button in the following code: <input type="button" value="BLUE" name="button_blue" /> My goal is to have the value="BLUE" changed to value="RED" or any other desired value when the button is clicked. ...

Is there a way to convert an empty string to zero in MySQL?

Can you help with answering my question? My question pertains to saving an empty string "" value in my table and storing it as a 0 value in the MySQL database. Here is a visual representation: Table -> MySQL "" 0 Thank you for your assi ...

Is it more advantageous in Vue to pre-process and save data directly to the data property, or to utilize computed properties?

I am interested in hearing diverse perspectives on this topic. When working with Vue (and possibly other frameworks), is it more beneficial to prepare non-reactive data through a method and store it in the data for use in the template, or is it preferable ...

Would it be secure to store the Express Session Secret as plain text while using it with Angular inside a Docker Container?

Upon taking over a new project, I noticed that the front end docker container has been set up in the following manner. Although this may seem like a basic question, I am still getting the hang of working with Angular/Express/Nodejs. FROM node:18.12.1 ...

Unable to detect hover (etc) events after generating div elements with innerHTML method

After using the code below to generate some divs document.getElementById('container').innerHTML += '<div class="colorBox" id="box'+i+'"></div>'; I am encountering an issue with capturing hover events: $(".colorB ...

How can I secure my website routes for authenticated users using NextAuth in Next.js?

Ensuring that all routes on my NextJs website are protected for authenticated users with NextAuth, except the login route which should be accessible without authentication, posed a challenge. I attempted to create a middleware to validate user tokens and r ...