Skip to content

Commit 286fa8b

Browse files
committed
Initial commit
0 parents  commit 286fa8b

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
node_modules

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# domain-file-server
2+
3+
_This software has not been tested in regards of security and should not be used
4+
in a production environment_
5+
6+
**domain-file-server** is a simplistic Nodejs file server supporting multiple
7+
domains. It is mainly intended to run an internal network with its own DNS server.
8+
9+
## Configuration
10+
11+
Create an asset folder, named `build` by default. In this folder create one
12+
folder for each domain that should be served. These folders should contain the
13+
files served on the respective domains. For example:
14+
15+
```sh
16+
build
17+
build/localhost
18+
build/localhost/favicon.ico
19+
build/localhost/index.html
20+
build/mybusiness.com
21+
build/mybusiness.com/favicon.ico
22+
build/mybusiness.com/index.html
23+
build/mybusiness.com/script.js
24+
build/mybusiness.com/styles.css
25+
build/mycat.com
26+
build/mycat.com/favicon.ico
27+
build/mycat.com/index.html
28+
build/mycat.com/script.js
29+
```

package-lock.json

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "domain-file-server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"prettier": "^2.7.1"
14+
}
15+
}

server.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const fs = require("fs");
2+
const http = require("http");
3+
const path = require("path");
4+
5+
const DEFAULT_INDEX_FILE = process.env.DEFAULT_INDEX_FILE || "index.html";
6+
const ROOT_FILE_PATH = process.env.ROOT_FILE_PATH || "build";
7+
8+
function getFile(filePath) {
9+
try {
10+
const file = fs.readFileSync(filePath);
11+
return file;
12+
} catch {
13+
console.error(`Could not find file "${filePath}"`);
14+
return;
15+
}
16+
}
17+
18+
function getDomainFromRequest(req) {
19+
const headers = req.headers;
20+
const host = headers.host;
21+
const domain = host.split(":")[0];
22+
return domain;
23+
}
24+
25+
function getFilePathFromRequest(req) {
26+
return req.url.split("?")[0];
27+
}
28+
29+
function createFilePath(domain, filePath) {
30+
if (filePath === "/") {
31+
filePath = DEFAULT_INDEX_FILE;
32+
}
33+
return path.join(ROOT_FILE_PATH, domain, filePath);
34+
}
35+
36+
function handleRequest(req, res) {
37+
const domain = getDomainFromRequest(req);
38+
const filePath = getFilePathFromRequest(req);
39+
console.debug("Requesting", domain, filePath);
40+
41+
const actualFilePath = createFilePath(domain, filePath);
42+
const file = getFile(actualFilePath);
43+
if (!file) {
44+
res.writeHead(404);
45+
res.end();
46+
return;
47+
}
48+
res.writeHead(200);
49+
res.end(file);
50+
}
51+
52+
const server = http.createServer(handleRequest);
53+
server.listen(8080);

0 commit comments

Comments
 (0)