2024-04-16 14:05:34 +00:00
|
|
|
const express = require("express");
|
|
|
|
const multer = require("multer");
|
|
|
|
const path = require("path");
|
2024-04-16 15:15:06 +00:00
|
|
|
const cors = require("cors"); // Import the cors middleware
|
2024-04-16 14:05:34 +00:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
2024-04-16 15:15:06 +00:00
|
|
|
app.use(cors()); // Enable CORS for all routes
|
|
|
|
|
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
console.error(err.stack);
|
|
|
|
res.status(500).send("Something broke!");
|
|
|
|
});
|
|
|
|
|
2024-04-16 14:05:34 +00:00
|
|
|
// 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) => {
|
2024-04-16 15:15:06 +00:00
|
|
|
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" });
|
|
|
|
}
|
2024-04-16 14:05:34 +00:00
|
|
|
});
|