LOADING
Upload video, audio, dan gambar ke CDN. unggah.web.id
import fs from "fs";
import path from "path";
import axios from "axios";
import FormData from "form-data";
async function uploadFile(filePath) {
const fileName = path.basename(filePath);
const form = new FormData();
form.append("file", fs.createReadStream(filePath), fileName);
try {
const res = await axios.post("https://unggah.web.id/api/unggah", form, {
headers: form.getHeaders()
});
return res.data;
} catch (err) {
if (err.response) {
return { error: "request gagal", status: err.response.status, body: String(err.response.data).slice(0, 200) };
}
return { error: err.message };
}
}
const filePath = process.argv[2];
if (!filePath) {
console.log(JSON.stringify({ error: "path file diperlukan" }, null, 2));
process.exit(1);
}
uploadFile(filePath)
.then((data) => console.log(JSON.stringify(data, null, 2)))
.catch((err) => console.log(JSON.stringify({ error: err.message }, null, 2)));installnpm install axios form-data
usenode unggah.js media/with path
example{ "url": "https://unggah.web.id/NSghyoMIuqMP.jpg", "fileName": "gambar.jpg", "size": 49812 }