-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
32 lines (27 loc) · 944 Bytes
/
server.mjs
File metadata and controls
32 lines (27 loc) · 944 Bytes
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
import fs from 'node:fs/promises';
import http from 'node:http';
import path from 'node:path';
const server = http.createServer(async (req, res) => {
switch (req.url) {
case '/':
const indexHtmlPath = path.resolve('index.html');
const html = await fs.readFile(indexHtmlPath, 'utf-8');
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
res.end(html);
break;
case '/cat':
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(getRandomCatName());
break;
default:
res.writeHead(404).end();
break;
}
});
server.listen(3000);
console.log('🤖 SERVER STARTED.');
function getRandomCatName() {
// These are all real cats.
const catNames = ['Gerald', 'Sadie', 'Fat Tina', 'Fufu', 'Tofu', 'Chairman Meow', 'Whiskers', 'Deckard', 'Walter', 'Annie', 'Razzy', 'Tiger', 'Shadow'];
return catNames[Math.floor(Math.random() * catNames.length)];
}