From 2ace1f689cd1e03ef76a61e84805b5455e0dafe9 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Tue, 16 Apr 2024 11:15:06 -0400 Subject: [PATCH] no errors, upload succsessful --- .../oceanslim/{merge.png => test.png} | Bin index.js | 11 ++++++---- server.js | 19 +++++++++++++++--- 3 files changed, 23 insertions(+), 7 deletions(-) rename .nip05Storage/oceanslim/{merge.png => test.png} (100%) diff --git a/.nip05Storage/oceanslim/merge.png b/.nip05Storage/oceanslim/test.png similarity index 100% rename from .nip05Storage/oceanslim/merge.png rename to .nip05Storage/oceanslim/test.png diff --git a/index.js b/index.js index 6f22d50..f363f42 100644 --- a/index.js +++ b/index.js @@ -99,7 +99,7 @@ const serverUrl = "http://localhost:3000"; // Adjust the port as needed async function handleUpload() { const fileInput = document.querySelector('input[type="file"]'); const selectedFile = fileInput.files[0]; - const nip05Username = localStorage.getItem("nip05Username"); // Assuming you have a function to get nip05Username + const nip05Username = localStorage.getItem("nip05Username"); const formData = new FormData(); formData.append("file", selectedFile); @@ -114,13 +114,16 @@ async function handleUpload() { ); if (response.ok) { - console.log("File uploaded successfully"); + const data = await response.json(); + console.log("File uploaded successfully:", data); // Handle success } else { - throw new Error("Failed to upload file"); + const errorMessage = await response.text(); // Get error message from response + console.error("Error uploading file. Server response:", errorMessage); + // Handle error } } catch (error) { console.error("Error uploading file:", error); - // Handle error + // Handle network errors } } diff --git a/server.js b/server.js index 533f0e8..d645172 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,17 @@ 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) { @@ -23,7 +31,12 @@ app.listen(3000, () => { }); app.post("/upload", upload.single("file"), (req, res) => { - // The file has been uploaded to the destination folder - // Respond with success status - res.sendStatus(200); + 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" }); + } });