Spaces:
Sleeping
Sleeping
Create server/index.ts
Browse files- server/index.ts +28 -0
server/index.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import multer from "multer";
|
| 3 |
+
import fs from "fs";
|
| 4 |
+
import { uploadToHF } from "./upload";
|
| 5 |
+
|
| 6 |
+
const app = express();
|
| 7 |
+
const upload = multer({ dest: "tmp/" });
|
| 8 |
+
|
| 9 |
+
app.post("/api/upload", upload.single("file"), async (req, res) => {
|
| 10 |
+
try {
|
| 11 |
+
if (!req.file) return res.status(400).json({ error: "No file" });
|
| 12 |
+
|
| 13 |
+
const url = await uploadToHF(
|
| 14 |
+
req.file.path,
|
| 15 |
+
req.file.originalname
|
| 16 |
+
);
|
| 17 |
+
|
| 18 |
+
fs.unlinkSync(req.file.path);
|
| 19 |
+
|
| 20 |
+
res.json({ url });
|
| 21 |
+
} catch (e: any) {
|
| 22 |
+
res.status(500).json({ error: e.message });
|
| 23 |
+
}
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
app.listen(3000, () => {
|
| 27 |
+
console.log("Backend running :3000");
|
| 28 |
+
});
|