-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (27 loc) · 854 Bytes
/
server.js
File metadata and controls
35 lines (27 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import express from "express";
import fetch from "node-fetch";
import FormData from "form-data";
const app = express();
app.get("/", async (req, res) => {
const imageUrl = req.query.u;
if (!imageUrl) return res.send("No image URL");
const form = new FormData();
form.append("image_url", imageUrl);
form.append("size", "auto");
const r = await fetch("https://api.remove.bg/v1.0/removebg", {
method: "POST",
headers: { "X-Api-Key": process.env.REMOVEBG_KEY },
body: form
});
if (!r.ok) {
const err = await r.text();
return res.status(500).send("RemoveBG failed: " + err);
}
const buf = await r.buffer();
res.setHeader("Content-Type", "image/png");
res.setHeader("Content-Disposition", "attachment; filename=removed-bg.png");
res.send(buf);
});
app.listen(3000, () =>
console.log("Server running")
);