updated server logic
Browse files
server.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const multer = require('multer');
|
| 3 |
const cors = require('cors');
|
| 4 |
-
const imageProcessor = require('./image-processing-logic');
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const PORT = process.env.PORT || 7860;
|
| 8 |
|
|
|
|
| 9 |
app.use(cors());
|
| 10 |
app.use(express.json());
|
| 11 |
|
| 12 |
const storage = multer.memoryStorage();
|
| 13 |
-
const upload = multer({
|
| 14 |
storage: storage,
|
| 15 |
-
limits: { fileSize: 15 * 1024 * 1024 },
|
| 16 |
fileFilter: (req, file, cb) => {
|
| 17 |
if (file.mimetype.startsWith('image/')) {
|
| 18 |
cb(null, true);
|
|
@@ -22,11 +23,18 @@ const upload = multer({
|
|
| 22 |
}
|
| 23 |
});
|
| 24 |
|
|
|
|
| 25 |
app.get('/ping', (req, res) => {
|
| 26 |
res.status(200).send('pong');
|
| 27 |
});
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try {
|
| 31 |
if (!req.file) {
|
| 32 |
return res.status(400).json({ error: 'No images uploaded' });
|
|
@@ -43,17 +51,32 @@ app.post('/api/process', upload.single('images'), async (req, res) => {
|
|
| 43 |
|
| 44 |
res.setHeader('Content-Type', `image/${format}`);
|
| 45 |
res.setHeader('Content-Disposition', `attachment; filename=compressed_${req.file.originalname}`);
|
| 46 |
-
res.send(processedImage.buffer);
|
| 47 |
|
| 48 |
} catch (error) {
|
| 49 |
-
|
| 50 |
-
|
| 51 |
}
|
| 52 |
});
|
| 53 |
|
|
|
|
|
|
|
| 54 |
app.use((err, req, res, next) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
console.error(err.stack);
|
| 56 |
-
res.status(500).json({ error:
|
| 57 |
});
|
| 58 |
|
| 59 |
app.listen(PORT, () => {
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const multer = require('multer');
|
| 3 |
const cors = require('cors');
|
| 4 |
+
const imageProcessor = require('./image-processing-logic');
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const PORT = process.env.PORT || 7860;
|
| 8 |
|
| 9 |
+
// Enable CORS for all origins. This is crucial for a public API.
|
| 10 |
app.use(cors());
|
| 11 |
app.use(express.json());
|
| 12 |
|
| 13 |
const storage = multer.memoryStorage();
|
| 14 |
+
const upload = multer({
|
| 15 |
storage: storage,
|
| 16 |
+
limits: { fileSize: 15 * 1024 * 1024 }, // 15MB file size limit
|
| 17 |
fileFilter: (req, file, cb) => {
|
| 18 |
if (file.mimetype.startsWith('image/')) {
|
| 19 |
cb(null, true);
|
|
|
|
| 23 |
}
|
| 24 |
});
|
| 25 |
|
| 26 |
+
// A simple endpoint to confirm the server is running.
|
| 27 |
app.get('/ping', (req, res) => {
|
| 28 |
res.status(200).send('pong');
|
| 29 |
});
|
| 30 |
|
| 31 |
+
// A root path to prevent "Cannot GET /" errors.
|
| 32 |
+
app.get('/', (req, res) => {
|
| 33 |
+
res.status(200).send('Image compression backend is running!');
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
// Main image processing route
|
| 37 |
+
app.post('/api/process', upload.single('images'), async (req, res, next) => {
|
| 38 |
try {
|
| 39 |
if (!req.file) {
|
| 40 |
return res.status(400).json({ error: 'No images uploaded' });
|
|
|
|
| 51 |
|
| 52 |
res.setHeader('Content-Type', `image/${format}`);
|
| 53 |
res.setHeader('Content-Disposition', `attachment; filename=compressed_${req.file.originalname}`);
|
| 54 |
+
res.status(200).send(processedImage.buffer);
|
| 55 |
|
| 56 |
} catch (error) {
|
| 57 |
+
// Pass any caught errors to the main error handler
|
| 58 |
+
next(error);
|
| 59 |
}
|
| 60 |
});
|
| 61 |
|
| 62 |
+
// Global error handling middleware.
|
| 63 |
+
// This handles all errors from the application, including Multer errors.
|
| 64 |
app.use((err, req, res, next) => {
|
| 65 |
+
// Check for specific Multer errors
|
| 66 |
+
if (err instanceof multer.MulterError) {
|
| 67 |
+
if (err.code === 'LIMIT_FILE_SIZE') {
|
| 68 |
+
return res.status(413).json({ error: 'File is too large. Max size is 15MB.' });
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
// Handle other custom errors from the fileFilter or general route logic
|
| 73 |
+
if (err.message === 'Only image files are allowed!') {
|
| 74 |
+
return res.status(415).json({ error: err.message });
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// Default error handling for any other unhandled errors
|
| 78 |
console.error(err.stack);
|
| 79 |
+
res.status(500).json({ error: 'An unexpected error occurred.' });
|
| 80 |
});
|
| 81 |
|
| 82 |
app.listen(PORT, () => {
|