-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·64 lines (51 loc) · 1.54 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
const http = require('patchbay-http');
const url = require('url');
const fs = require('fs');
const path = require('path');
const process = require('process');
const mime = require('mime');
if (process.argv.length < 3) {
throw new Error("Not enough args");
}
const rootChannel = process.argv[2];
const srv = http.createServer(async (req, res) => {
const rootDir = './';
const urlParts = url.parse(req.url);
res.setHeader('Pb-Res-Content-Type', mime.getType(req.url));
try {
const childPath = path.join(rootDir, urlParts.pathname.slice('/req'.length + rootChannel.length));
const stats = await fs.promises.stat(childPath);
if (stats.isFile()) {
const stream = fs.createReadStream(childPath);
stream.pipe(res);
}
else {
const indexPath = path.join(childPath, 'index.html');
try {
const indexStats = await fs.promises.stat(indexPath);
res.setHeader('Pb-Res-Content-Type', 'text/html');
const indexStream = fs.createReadStream(indexPath);
indexStream.pipe(res);
}
catch (e) {
console.log(e);
res.setHeader('Pb-Status', '404');
res.write("Not found");
res.end();
}
}
}
catch (e) {
console.error(e);
res.setHeader('Pb-Status', '404');
res.write("Not found");
res.end();
}
});
srv.setPatchbayServer('https://beta.patchbay.pub');
//srv.setPatchbayServer('http://localhost:9001');
srv.setPatchbayChannel(rootChannel);
// TODO: implement setNumWorkers
//srv.setNumWorkers(4);
srv.listen(3000);