basic-node-server 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/bns.js +35 -3
- package/index.html +12 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Basic node server for everyone.
|
|
3
3
|
|
|
4
4
|
Basic node server simply hosts and serves files from the current project directory through HTTP.
|
|
5
|
-
If no path is given, it will serve the index.html file in the
|
|
5
|
+
If no path is given, or if the path ends with '/' or '\', it will serve the index.html file in the directory.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -24,5 +24,5 @@ The [port] argument is optional. If no port is given, it will default to 3000.
|
|
|
24
24
|
For example, if you want to serve the current directory on port 8080, you would run:
|
|
25
25
|
|
|
26
26
|
```
|
|
27
|
-
|
|
27
|
+
npx bns 8080
|
|
28
28
|
```
|
package/bns.js
CHANGED
|
@@ -5,7 +5,7 @@ const fs = require('fs');
|
|
|
5
5
|
const path = require('path');
|
|
6
6
|
|
|
7
7
|
const port = process.argv[2] || 3000; // Set the port from the command line argument or default to 3000
|
|
8
|
-
|
|
8
|
+
const notFoundFile = process.argv?.[3] || null;
|
|
9
9
|
const mimeJson = path.join(__dirname, '/mime.json');
|
|
10
10
|
|
|
11
11
|
const getContentType = (() => {
|
|
@@ -43,8 +43,40 @@ const server = http.createServer((req, res) => {
|
|
|
43
43
|
fs.access(filePath, fs.constants.F_OK, (err) => {
|
|
44
44
|
if (err) {
|
|
45
45
|
// File not found
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
if (notFoundFile) {
|
|
47
|
+
filePath = path.join(process.cwd(), notFoundFile);
|
|
48
|
+
fs.access(filePath, fs.constants.F_OK, (err) => {
|
|
49
|
+
if (err) {
|
|
50
|
+
res.statusCode = 404;
|
|
51
|
+
res.end('File not found');
|
|
52
|
+
} else {
|
|
53
|
+
// Read the file and send it as the response
|
|
54
|
+
const fileExtension = path.extname(filePath).substring(1);
|
|
55
|
+
getContentType.then(contentType => {
|
|
56
|
+
console.log(`Serving: ${filePath}`);
|
|
57
|
+
|
|
58
|
+
contentType = contentType[fileExtension] || 'application/octet-stream';
|
|
59
|
+
res.setHeader('Content-Type', contentType);
|
|
60
|
+
|
|
61
|
+
fs.readFile(filePath, (err, data) => {
|
|
62
|
+
if (err) {
|
|
63
|
+
// Error reading the file
|
|
64
|
+
res.statusCode = 500;
|
|
65
|
+
res.end('Internal server error');
|
|
66
|
+
} else {
|
|
67
|
+
// Send the file contents as the response
|
|
68
|
+
res.statusCode = 200;
|
|
69
|
+
res.end(data);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
res.statusCode = 404;
|
|
78
|
+
res.end('File not found');
|
|
79
|
+
}
|
|
48
80
|
} else {
|
|
49
81
|
// Read the file and send it as the response
|
|
50
82
|
const fileExtension = path.extname(filePath).substring(1);
|
package/index.html
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
<h2>How to use</h2>
|
|
14
14
|
<pre>
|
|
15
|
-
|
|
15
|
+
node bns [port] [404 file]
|
|
16
16
|
</pre>
|
|
17
17
|
<p>
|
|
18
18
|
The [port] argument is optional. If no port is given, it will default to 3000.
|
|
@@ -20,16 +20,23 @@
|
|
|
20
20
|
For example, if you want to serve the current directory on port 8080, you would run:
|
|
21
21
|
</p>
|
|
22
22
|
<pre>
|
|
23
|
-
|
|
23
|
+
node bns 8080
|
|
24
|
+
</pre>
|
|
25
|
+
<p>
|
|
26
|
+
The [404 file] argument is optional. If file location is given, it will respond the file on 404.
|
|
27
|
+
<br>
|
|
28
|
+
For example, if you want your 404 page to be 404.html on port 8080, you would run:
|
|
29
|
+
</p>
|
|
30
|
+
<pre>
|
|
31
|
+
node bns 8080 404.html
|
|
24
32
|
</pre>
|
|
25
|
-
|
|
26
33
|
<h2>NPM</h2>
|
|
27
34
|
<pre>
|
|
28
|
-
|
|
35
|
+
npm install basic-node-server
|
|
29
36
|
</pre>
|
|
30
37
|
<p>
|
|
31
38
|
Then, you can run it from your project directory:
|
|
32
39
|
</p>
|
|
33
40
|
<pre>
|
|
34
|
-
|
|
41
|
+
npx bns [port] [404 file]
|
|
35
42
|
</pre>
|