-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (41 loc) · 1.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 8000;
app.use(express.static('public'));
app.use(bodyParser.text());
// Custom page for Starry! 💫
app.get('/starry', (req, res) => {
const content = fs.readFileSync(path.join(__dirname, 'starry.lua'), 'utf8');
res.send(content);
})
app.post('/raw', (req, res) => {
const codeContent = req.body;
if (codeContent.trim() !== "") {
const fileName = generateRandomFileName();
const filePath = path.join(__dirname, 'public', 'raw', `${fileName}.txt`);
fs.writeFile(filePath, codeContent, (err) => {
if (err) {
console.error(err);
res.status(500).send('Internal Server Error');
} else {
return res.send(`${fileName}.txt`);
}
});
} else {
res.status(400).send('Bad Request: Please provide code content.');
}
});
function generateRandomFileName() {
const characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let result = "";
for (let i = 0; i < 8; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});