-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·66 lines (56 loc) · 1.48 KB
/
cli.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
65
66
#!/usr/bin/env node
"use strict";
const http = require("http");
const fs = require("fs");
const mime = require("mime-types");
const localtunnel = require("localtunnel");
const { hostname, port, root, subdomain } = require("minimist")(
process.argv.slice(2),
{
string: ["hostname", "root", "subdomain"],
default: {
hostname: "127.0.0.1",
port: 5000
},
alias: {
hostname: "h",
port: "p",
root: "r",
subdomain: "s"
}
}
);
if (root == null) {
console.error("You need to provide --root or -r argument");
process.exit();
}
const server = http.createServer((req, res) => {
const path = `${root}/${req.url}`;
if (!fs.existsSync(path)) {
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain");
res.end("Not found");
} else if (!fs.statSync(path).isFile()) {
res.statusCode = 400;
res.setHeader("Content-Type", "text/plain");
res.end("Cannot read directories");
} else {
res.statusCode = 200;
res.setHeader("Content-Type", mime.lookup(path));
res.write(fs.readFileSync(path));
res.end();
}
});
server.listen(port, hostname, () => {
console.log(`Local server is running at http://${hostname}:${port}/`);
});
const tunnel = localtunnel(port, { subdomain }, (err, tunnel) => {
if (err) {
console.error(err);
process.exit();
}
console.info(`Public URL ${tunnel.url} is pointed to local folder '${root}'`);
});
tunnel.on("close", () => {
console.info("Tunnel closed");
});