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
.