Spaces:
Runtime error
Runtime error
Create api.js
Browse files
api.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const fs = require('fs').promises;
|
| 3 |
+
const path = require('path');
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
app.use(express.json());
|
| 7 |
+
|
| 8 |
+
const PORT = 6666;
|
| 9 |
+
|
| 10 |
+
// JSON processing endpoint
|
| 11 |
+
app.post('/api/process', async (req, res) => {
|
| 12 |
+
try {
|
| 13 |
+
const inputData = req.body;
|
| 14 |
+
|
| 15 |
+
// Example processing - echoes input with timestamp
|
| 16 |
+
const response = {
|
| 17 |
+
received: inputData,
|
| 18 |
+
timestamp: new Date().toISOString(),
|
| 19 |
+
processed: true
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
res.json(response);
|
| 23 |
+
} catch (error) {
|
| 24 |
+
res.status(500).json({ error: 'Processing failed', message: error.message });
|
| 25 |
+
}
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
// Image serving endpoint
|
| 29 |
+
|
| 30 |
+
/*
|
| 31 |
+
app.get('/api/image', async (req, res) => {
|
| 32 |
+
try {
|
| 33 |
+
// Create a simple test image if it doesn't exist
|
| 34 |
+
const imagePath = path.join(__dirname, 'test-image.png');
|
| 35 |
+
|
| 36 |
+
try {
|
| 37 |
+
await fs.access(imagePath);
|
| 38 |
+
} catch {
|
| 39 |
+
// If image doesn't exist, create a placeholder
|
| 40 |
+
const { createCanvas } = require('canvas');
|
| 41 |
+
const canvas = createCanvas(200, 200);
|
| 42 |
+
const ctx = canvas.getContext('2d');
|
| 43 |
+
|
| 44 |
+
// Draw something simple
|
| 45 |
+
ctx.fillStyle = '#3498db';
|
| 46 |
+
ctx.fillRect(0, 0, 200, 200);
|
| 47 |
+
|
| 48 |
+
ctx.fillStyle = '#ffffff';
|
| 49 |
+
ctx.font = '20px Arial';
|
| 50 |
+
ctx.fillText('Test Image', 50, 100);
|
| 51 |
+
|
| 52 |
+
// Save the image
|
| 53 |
+
const buffer = canvas.toBuffer('image/png');
|
| 54 |
+
await fs.writeFile(imagePath, buffer);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// Read and send the image
|
| 58 |
+
const imageBuffer = await fs.readFile(imagePath);
|
| 59 |
+
res.set('Content-Type', 'image/png');
|
| 60 |
+
res.send(imageBuffer);
|
| 61 |
+
} catch (error) {
|
| 62 |
+
res.status(500).json({ error: 'Image serving failed', message: error.message });
|
| 63 |
+
}
|
| 64 |
+
});
|
| 65 |
+
**
|
| 66 |
+
app.listen(PORT, () => {
|
| 67 |
+
console.log(`API server running on port ${PORT}`);
|
| 68 |
+
});
|