nostrMediaUpload/server.js

43 lines
1.2 KiB
JavaScript

const express = require("express");
const multer = require("multer");
const path = require("path");
const cors = require("cors"); // Import the cors middleware
const app = express();
app.use(cors()); // Enable CORS for all routes
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something broke!");
});
// Set up multer for handling file uploads
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const nip05Username = req.query.nip05Username; // Extract nip05Username from query params
const uploadPath = path.join(__dirname, ".nip05Storage", nip05Username);
cb(null, uploadPath);
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
const upload = multer({ storage: storage });
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
app.post("/upload", upload.single("file"), (req, res) => {
try {
// The file has been uploaded to the destination folder
// Respond with success status and a JSON response
res.status(200).json({ message: "File uploaded successfully" });
} catch (error) {
console.error("Error uploading file:", error);
res.status(500).json({ error: "Error uploading file" });
}
});