Create app.js
Browse files
app.js
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const fs = require('fs');
|
3 |
+
const path = require('path');
|
4 |
+
|
5 |
+
const app = express();
|
6 |
+
const port = 7860;
|
7 |
+
const imageFolderPath = path.join(__dirname, 'api');
|
8 |
+
|
9 |
+
app.get('/', (req, res) => {
|
10 |
+
res.send('api');
|
11 |
+
});
|
12 |
+
|
13 |
+
app.get('/r18', (req, res) => {
|
14 |
+
const r18FolderPath = path.join(__dirname, 'api', 'r18');
|
15 |
+
const images = fs.readdirSync(r18FolderPath);
|
16 |
+
const randomImage = getRandomImage(images);
|
17 |
+
res.sendFile(path.join(r18FolderPath, randomImage));
|
18 |
+
});
|
19 |
+
|
20 |
+
function getRandomImage(images) {
|
21 |
+
const randomIndex = Math.floor(Math.random() * images.length);
|
22 |
+
return images[randomIndex];
|
23 |
+
}
|
24 |
+
|
25 |
+
app.listen(port, () => {
|
26 |
+
console.log(`App listening on port ${port}!`);
|
27 |
+
});
|