LearnexUplImgApi / server.js
Rooni's picture
Update server.js
291ad85 verified
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const app = express();
const PORT = process.env.PORT || 8000; // Используем переменную окружения PORT
const CLIENT_ID = process.env.CLIENT_ID;
app.use(express.json({ limit: '10mb' }));
app.post('/upload', async (req, res) => {
try {
const { b64, name } = req.body;
if (!b64) {
return res.status(400).send('Base64 image data is required in "b64" field.');
}
const response = await axios.post('https://api.imageban.ru/v1',
{
image: b64, // Используем поле "b64"
name: name || 'uploaded_image'
},
{
headers: {
'Authorization': `TOKEN ${CLIENT_ID}`, // Авторизация через CLIENT_ID
}
});
const { data } = response.data;
const imageLink = data[0].link;
res.send({ url: imageLink });
} catch (error) {
console.error('Error uploading image:', error);
res.status(500).send({ error: 'Internal server error' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});