-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (68 loc) · 2.42 KB
/
server.js
File metadata and controls
75 lines (68 loc) · 2.42 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const http = require('http');
const fs = require('fs')
const url = require('url');
const querystring = require('querystring');
const figlet = require('figlet');
const PORT = process.env.PORT || 8000;
const server = http.createServer((req, res) => {
const page = url.parse(req.url).pathname;
const params = querystring.parse(url.parse(req.url).query);
console.log(page);
if (page == '/') {
fs.readFile('index.html', (err, data) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}
else if (page == '/api') {
// Define character sets
const numbers = '0123456789';
const letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const specialChars = '!@#$%^&*_+-?';
// Generate random length between 8 and 16
const length = Math.floor(Math.random() * 9) + 8;
// Initialize password with at least one character from each set
let password = '';
password += numbers[Math.floor(Math.random() * numbers.length)];
password += letters[Math.floor(Math.random() * letters.length)];
password += specialChars[Math.floor(Math.random() * specialChars.length)];
// Fill the rest of the password
const allChars = numbers + letters + specialChars;
for (let i = password.length; i < length; i++) {
password += allChars[Math.floor(Math.random() * allChars.length)];
}
// Shuffle the password
password = password.split('').sort(() => Math.random() - 0.5).join('');
res.writeHead(200, {'Content-Type': 'application/json'});
const objToJson = {
status: "success",
password: password
}
res.end(JSON.stringify(objToJson));
}
else if (page == '/css/style.css') {
fs.readFile('css/style.css', (err, data) => {
res.writeHead(200, {'Content-Type': 'text/css'});
res.write(data);
res.end();
});
} else if (page == '/js/main.js') {
fs.readFile('js/main.js', (err, data) => {
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.write(data);
res.end();
});
} else {
figlet('R__U__LOST? 404', (err, result) => {
if (err) {
console.log('Something went wrong...');
console.dir(err);
return;
}
res.write(result);
res.end();
});
}
});
server.listen(PORT);