Upload an image to a Node.js API using the Next.js API directory

I am working with a file instance that I obtained from the formidable library. Here's how it looks:

photo: File {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    size: 16648,
    path: 'public/upload_1.jpg',
    name: 'test.jpg',
    type: 'image/jpeg',
    hash: null,
    lastModifiedDate: 2021-07-08T11:22:05.804Z,
    _writeStream: WriteStream {
      _writableState: [WritableState],
      writable: false,
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      path: 'public/upload_1.jpg',
      fd: null,
      flags: 'w',
      mode: 438,
      start: undefined,
      autoClose: true,
      pos: undefined,
      bytesWritten: 16648, 
      closed: false
    }
  }

Now, I need to send this image to an external Node.js API which expects the image in the format of:

req.file('photo')

Could someone please guide me on how to properly send the above instance to the Node.js API?

Thank you in advance!

Answer №1

Utilizing Serverside FormData and fs.createReadStream proved to be incredibly effective for me.

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

Using Node.js to retrieve table data from a URL

My journey with Node JS and express is just beginning as I dive into building a website that serves static files. Through my research, I discovered the potential of using NodeJS with Express for this purpose. While I have successfully served some static HT ...

Guide to using Node.js to efficiently add documents to several collections in a single query

I am exploring the possibility of inserting documents into multiple collections with a single query. Let's say I have two collections named person and address. My goal is to insert documents into both collections using just one query. Specifically, d ...

Searching in binary for the number closest to the target float value

Seeking assistance with a float array conundrum! I am trying to identify the closest float value to a target number within the array. My attempt involved utilizing a basic binary search algorithm in JavaScript, but I've hit a roadblock. function bina ...

Can an onload function be triggered within the location.href command?

Can a function be called onload in the location.href using jQuery? location.href = getContextPath() + "/home/returnSeachResult?search=" + $('#id-search-text-box').val() + "&category=" + $('#search_concept').text() + "onload='j ...

I'm currently attempting to set up the React JS package, but unfortunately encountering some errors in the process

I'm having trouble installing react as I keep receiving errors. My system has npm version 8.12.1 installed. I attempted to downgrade react, but that didn't resolve the issue. I tried the following steps: Update npm using: npm install npm -g Dow ...

Retrieve data from a list using an API

I am currently working on creating a dynamic list by fetching data from an API. The goal is to display detailed information in a modal when a user clicks on a specific item in the list. While the list itself is functioning properly, I am encountering an i ...

Error: Package [email protected] could not be located

While attempting to launch my project, I encountered an unexpected error message. npm ERR! 404 Not Found: [email protected] ...

NextJs is throwing an error saying that it is unable to read properties of undefined when trying to access 'clearStorage'

I'm currently working on implementing Zustand persist in my Next.js component for local storage functionality. However, I have encountered an issue with a Shadcn UI button causing errors to appear in my terminal: components/stateInput.tsx (40:75) @ cl ...

What is the best way to retrieve JSON values based on a key using JavaScript, jQuery, or AngularJS?

Here is some JSON data that I need help with: var jsonData = { "title": "Testing", "settings": { "mySettings": false }, "jsonList": ["TestingList"], "testJsonVals": { "Test1": { "name": "name1", ...

An unexpected error occurred in the Ember route processing: Assertion Failed in the listing

After working diligently to integrate an Emberjs front end (utilizing Ember Data) with my Flask API backend, I have encountered a puzzling issue. Despite the adapter functioning correctly - as evidenced by the API being called when accessing the 'List ...

The Angular2 app and NodeJs in the Docker container are unresponsive

After creating a new Angular2 app using angular-cli and running it in Docker, I encountered an issue where I could not connect to it from localhost. First, I initialized the app on my local machine: ng new project && cd project && "put m ...

Efficiently inserting batches in MongoDB using NodeJs

Currently, I am working with MongoDB version 2.6.9 and NodeJs version 0.10.37. In a previous inquiry related to calculating score from existing fields in MongoDB, I received detailed guidance from chridam. Here is the structured solution provided: var bul ...

Determine if an Android application has been installed using JavaScript or jQuery

I'm working on an Android app that is also accessible via a web browser. I want to add a banner prompting users to install the Android application if they haven't already. How can I use JavaScript or jQuery to detect if the user has the app insta ...

Verify if there are any similar sets within a set and proceed to either add or update in MongoDB

My goal is to access a specific array from my department's dStock collection. I intend to select it as I would any other data field in my primary collection, then make edits or deletions, or simply check for duplicates before adding new information. l ...

"Error: Attempting to send multiple res.send/json - headers cannot be set after they have already been sent

Is it possible to send multiple res.send/json in the same method? I am facing a challenge as I want to create a function within a Web-Worker to make a put request every minute. However, on the second request, I receive an error message "can't set head ...

Unable to display menu content text using jQuery

Currently experimenting with jQuery to create a dynamic submenu. The goal is to have a sub menu appear when the main menu is clicked, and then disappear when an item in the sub menu is selected, revealing additional information within a div. Unfortunately, ...

Utilizing ionic-scroll for seamless movement and scrolling of a canvas element

I have set up a canvas element with a large image and I want to enable dragging using ionic-scroll. Following the provided example: <ion-scroll zooming="true" direction="xy" style="width: 500px; height: 500px"> <div style="width: 5000px; h ...

What is the best way to indicate alternative dependencies in NPM?

When stating the requirements of a Debian package, you have the option to use syntax such as exim | mail-transport-agent to specify that your package requires either exim or mail-transport-agent, without any preference between the two. I am interested in ...

Columns that can be resized in a right-to-left (

Whenever I use RTL, the columns behave erratically when resizing... I have implemented colResizable. You can see an example here: http://jsfiddle.net/r0rfrhb7/ $("#nonFixedSample").colResizable({ fixed: false, liveDrag: true, gripInnerHtml: "<d ...

Utilize Axios to send data in real-time to update any changes made to an input field in

Just wanted to write about this topic because I have a burning question. (Please note that the code in this post is just an example). I'm struggling with sending the input content of name_contact on each change without using a button. Is it even poss ...