no errors, upload succsessful
This commit is contained in:
parent
85a689fb52
commit
2ace1f689c
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
11
index.js
11
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
|
||||
}
|
||||
}
|
||||
|
17
server.js
17
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) => {
|
||||
try {
|
||||
// The file has been uploaded to the destination folder
|
||||
// Respond with success status
|
||||
res.sendStatus(200);
|
||||
// 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" });
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user