Skip to content

Commit b073d93

Browse files
committed
mvp
1 parent c571c0e commit b073d93

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,67 @@
11
# srvd
22
Another Development Server. Supports Range Requests. Configure through Environmental Variables.
3+
4+
# basic usage
5+
```bash
6+
srvd
7+
```
8+
9+
# install
10+
```bash
11+
npm install -D srvd
12+
```
13+
14+
# advanced usage
15+
## in JavaScript
16+
```javascript
17+
const srvd = require("srvd");
18+
19+
// note: all options are optional
20+
const obj = srvd.serve({
21+
// whether to accept HTTP Range Requests to return partial files
22+
// default is true
23+
acceptRanges: true,
24+
25+
// set debug to true to see increased logging messages like what port is being used
26+
// default is false
27+
debug: true,
28+
29+
// port
30+
// default is 8088
31+
port: 3000,
32+
33+
// root directory to serve files from
34+
// default is the common working directory
35+
root: "/tmp/test"
36+
});
37+
```
38+
serve returns the following object:
39+
```js
40+
{
41+
// whether byte range requests are being served
42+
acceptRanges: true,
43+
44+
// whether debug logging is on/off
45+
debug: true,
46+
47+
// port server is running on
48+
port: 3000,
49+
50+
// root being used
51+
root: "/tmp/test",
52+
53+
// the http server object being used
54+
server: Server
55+
}
56+
```
57+
58+
# in the terminal
59+
You can just run `srvd`, but you have other options available:
60+
```bash
61+
srvd --accept-ranges=false --debug --port=8080 --root=$PWD/data
62+
```
63+
64+
# even more advanced usage
65+
## shutting the server down
66+
If you want to shut the server down (but not kill the main NodeJS process),
67+
you can run `server.close()` or setting the environmental variable `SRVD_PLZ_CLOSE` to `true` like `process.env.SRVD_PLZ_CLOSE=true`;

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "srvd",
3+
"version": "0.0.0",
4+
"description": "Another Development Server. Supports Range Requests. Configure through Environmental Variables.",
5+
"main": "srvd.js",
6+
"files": [
7+
"srvd.js"
8+
],
9+
"scripts": {
10+
"test": "node test.js"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/DanielJDufour/srvd.git"
15+
},
16+
"keywords": [
17+
"async",
18+
"byte",
19+
"config",
20+
"data",
21+
"dev",
22+
"env",
23+
"http",
24+
"range",
25+
"requests",
26+
"server",
27+
"static"
28+
],
29+
"author": "Daniel J. Dufour",
30+
"license": "CC0-1.0",
31+
"bugs": {
32+
"url": "https://github.com/DanielJDufour/srvd/issues"
33+
},
34+
"homepage": "https://github.com/DanielJDufour/srvd#readme"
35+
}

srvd.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const serveStatic = require("serve-static");
2+
const http = require("http");
3+
const finalhandler = require("finalhandler");
4+
5+
const DEFAULT_PORT = 8080;
6+
7+
function serve ({ acceptRanges = true, debug = false, root, port } = { acceptRanges: true }) {
8+
if (!root) {
9+
root = process.cwd();
10+
if (debug) console.log(`[srvd] root not set so using current working directory "${root}"`);
11+
}
12+
13+
14+
15+
if (!port) port = process.env.SRVD_DEFAULT_PORT ? process.env.SRVD_DEFAULT_PORT : DEFAULT_PORT;
16+
port = parseInt(port);
17+
18+
const serve = serveStatic(root, {
19+
acceptRanges
20+
});
21+
22+
const server = http.createServer(function onRequest(req, res) {
23+
serve(req, res, finalhandler(req, res));
24+
});
25+
26+
server.listen(port);
27+
if (debug) console.log("[srvd] serving on port " + port);
28+
29+
function checkForCloseRequest() {
30+
if (["TRUE","True","true"].includes(process.env.SRVD_PLZ_CLOSE)) {
31+
server.close();
32+
} else {
33+
setTimeout(() => checkForCloseRequest(), 500);
34+
}
35+
}
36+
setTimeout(() => checkForCloseRequest(), 500);
37+
38+
return { acceptRanges, debug, server, port, root };
39+
};
40+
41+
module.exports = { serve };
42+
43+
if (require.main === module) {
44+
const args = Array.from(process.argv);
45+
const str = args.join(" ");
46+
serve({
47+
debug: !!str.match(/-?-debug((=|== )(true|True|TRUE))?/),
48+
port: str.match(/-?-port(?:=|== )(\d+)/)?.[1],
49+
root: str.match(/-?-root(?:=|== )([^ ]+)/)?.[1]
50+
});
51+
}

test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const test = require("flug");
2+
const srvd = require("./srvd");
3+
4+
test("shutting down via callback", ({ eq }) => {
5+
const { server } = srvd.serve();
6+
server.close();
7+
});
8+
9+
test("shutting down via env", ({ eq }) => {
10+
srvd.serve();
11+
process.env.SRVD_PLZ_CLOSE = true;
12+
});

0 commit comments

Comments
 (0)