Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API request keeps failing #121

Open
almirchowdhury opened this issue May 15, 2024 · 0 comments
Open

API request keeps failing #121

almirchowdhury opened this issue May 15, 2024 · 0 comments

Comments

@almirchowdhury
Copy link

here is my backend:
import express from 'express';
import multer from 'multer';
import { removeBackground } from '@imgly/background-removal-node';

const router = express.Router();
const upload = multer({ storage: multer.memoryStorage() });

router.post('/remove-background', upload.single('image'), async (req, res) => {
try {
const imageBuffer = req.file.buffer;
const blob = await removeBackground(imageBuffer);

    const buffer = Buffer.from(await blob.arrayBuffer());
    const dataURL = `data:image/png;base64,${buffer.toString("base64")}`;
    
    res.json({ imageUrl: dataURL });
} catch (error) {
    res.status(500).json({ error: 'Failed to remove background', message: error.message });
}

});

export default router;

FE Implentation:
const handleRemoveBackground = async () => {
if (!imageFile) {
console.error('No image file available for background removal.');
return;
}

setLoading(true);
const formData = new FormData();
formData.append('image', imageFile);

try {
  const response = await fetch('http://localhost:8080/api/v1/background/remove-background', {
    method: 'POST',
    body: formData
  });
  if (!response.ok) {
    throw new Error('Network response was not ok');
  }
  const data = await response.json();
  setImageSrc(data.imageUrl);
} catch (error) {
  console.error('Error removing background:', error);
} finally {
  setLoading(false);
}

};
keep getting internal server error plz help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant