Categories
JavaScript Answers

How to use FormData in Node.js without browser with JavaScript?

Spread the love

To use FormData in Node.js without browser with JavaScript, we use the form-data module.

To install it, we run

npm i form-data

Then we write

const FormData = require("form-data");
const fs = require("fs");

const form = new FormData();
form.append("foo", "my value");
form.append("bar", new Buffer(10));
form.append("file", fs.createReadStream("/foo/bar.jpg"));

to create a FormData object.

We call append with the key and value of each entry.

And we set the value to a file by creating a read stream with createReadStream.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *