DeFactOfficial's picture
Update api.js
e6ef08b verified
const express = require('express');
const fs = require('fs').promises;
const path = require('path');
const app = express();
app.use(express.json());
const PORT = 6666;
// JSON processing endpoint
app.post('/api/process', async (req, res) => {
try {
const inputData = req.body;
// Example processing - echoes input with timestamp
const response = {
received: inputData,
timestamp: new Date().toISOString(),
processed: true
};
res.json(response);
} catch (error) {
res.status(500).json({ error: 'Processing failed', message: error.message });
}
});
// Image serving endpoint
/*
app.get('/api/image', async (req, res) => {
try {
// Create a simple test image if it doesn't exist
const imagePath = path.join(__dirname, 'test-image.png');
try {
await fs.access(imagePath);
} catch {
// If image doesn't exist, create a placeholder
const { createCanvas } = require('canvas');
const canvas = createCanvas(200, 200);
const ctx = canvas.getContext('2d');
// Draw something simple
ctx.fillStyle = '#3498db';
ctx.fillRect(0, 0, 200, 200);
ctx.fillStyle = '#ffffff';
ctx.font = '20px Arial';
ctx.fillText('Test Image', 50, 100);
// Save the image
const buffer = canvas.toBuffer('image/png');
await fs.writeFile(imagePath, buffer);
}
// Read and send the image
const imageBuffer = await fs.readFile(imagePath);
res.set('Content-Type', 'image/png');
res.send(imageBuffer);
} catch (error) {
res.status(500).json({ error: 'Image serving failed', message: error.message });
}
});*/
app.listen(PORT, () => {
console.log(`API server running on port ${PORT}`);
});