mirror of
https://github.com/NotAShelf/catApi.git
synced 2024-11-01 15:01:13 +00:00
this is it, this is the one
This commit is contained in:
parent
cc9f6beec8
commit
07aa9b1242
1 changed files with 95 additions and 0 deletions
95
index.js
Normal file
95
index.js
Normal file
|
@ -0,0 +1,95 @@
|
|||
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const fs = require('fs');
|
||||
require('dotenv').config();
|
||||
|
||||
const port = process.env.PORT;
|
||||
app.listen(port, () => {
|
||||
console.log('App listening at http://localhost:' + port);
|
||||
});
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(getRandomImagePath(), (err) => {
|
||||
if (err) {
|
||||
res.status(err.status).end();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/:id', (req, res) => {
|
||||
const image = getImageById(req.params.id);
|
||||
if (image) {
|
||||
res.sendFile(image, (err) => {
|
||||
if (err) {
|
||||
res.status(err.status).end();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return res.json({
|
||||
error: 'Image not found'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/listimgs', (req, res) => {
|
||||
return res.json(getAllImageIds());
|
||||
});
|
||||
|
||||
app.get('/api/randomimg', (req, res) => {
|
||||
return res.json(getRandomImageApi());
|
||||
});
|
||||
|
||||
app.get('/api/:id', (req, res) => {
|
||||
const image = getImageById(req.params.id, false);
|
||||
if (image) {
|
||||
return res.json({
|
||||
id: parseInt(req.params.id),
|
||||
url: 'https://cat.frozendev.tk' + req.params.id,
|
||||
});
|
||||
} else {
|
||||
return res.json({
|
||||
error: 'Image not found'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const getRandomImagePath = () => {
|
||||
const images = fs.readdirSync('./images');
|
||||
return __dirname + '/images/' + images[Math.floor(Math.random() * images.length)];
|
||||
};
|
||||
|
||||
const getImageById = (id, path = true) => {
|
||||
const images = fs.readdirSync('./images');
|
||||
|
||||
for (const image of images) {
|
||||
if (image.substring(0, image.length - 4).split('-')[1] === id) {
|
||||
if (path) {
|
||||
return __dirname + '/images/' + image;
|
||||
} else {
|
||||
return image;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const getAllImageIds = () => {
|
||||
const ids = [];
|
||||
|
||||
fs.readdirSync('./images').forEach(image => {
|
||||
ids.push(parseInt(image.substring(0, image.length - 4) .split('-')[1]));
|
||||
});
|
||||
|
||||
return ids.sort((a, b) => a - b);
|
||||
};
|
||||
|
||||
const getRandomImageApi = () => {
|
||||
const path = getRandomImagePath();
|
||||
const id = path.substring(0, path.length - 4).split('-')[1];
|
||||
return {
|
||||
id: parseInt(id),
|
||||
url: 'https://cat.frozendev.tk' + id,
|
||||
};
|
||||
};
|
Loading…
Reference in a new issue