-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
30 lines (26 loc) · 1.13 KB
/
server.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
import si from 'systeminformation';
import { createServer } from 'http';
const PORT = process.env.SI_PORT || 8098;
var server = createServer(async (request, response) => {
response.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
response.setHeader('Access-Control-Allow-Headers', 'Content-Type');
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Request-Method', '*');
try {
if (request.method === 'OPTIONS') {
response.writeHead(202);
response.end();
} else if (request.url == '/' || request.url == '/favicon.ico') {
response.writeHead(200);
response.end();
} else {
response.writeHead(200, { "Content-Type": "application/json" });
response.end(JSON.stringify({ ...await si[request.url.replace('/', '')]() }));
}
} catch (error) {
response.end(JSON.stringify({ error: error?.message + ' ' + error?.cause + ' ' + error?.stack }));
}
});
server.listen(PORT, function () {
console.log(`System Information server listening on: http://localhost:${PORT}`);
});