basic-node-server 1.0.13 → 1.0.15
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 +8 -2
- package/bns.js +48 -2
- package/index.html +12 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ npm install basic-node-server
|
|
|
15
15
|
Then, you can run it from your project directory:
|
|
16
16
|
|
|
17
17
|
```
|
|
18
|
-
npx bns [port] [404.page]
|
|
18
|
+
npx bns [port] 404=[404.page]
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
The [port] argument is optional. If no port is given, it will default to 3000.
|
|
@@ -29,5 +29,11 @@ npx bns 8080
|
|
|
29
29
|
If you need to setup additional 404 page you can do so by:
|
|
30
30
|
|
|
31
31
|
```
|
|
32
|
-
npx bns 8080 your404page.html
|
|
32
|
+
npx bns 8080 404=your404page.html
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
If you need to set response header to have no-caching settings:
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
npx bns 8080 404=your404page.html no-cache=true
|
|
33
39
|
```
|
package/bns.js
CHANGED
|
@@ -4,8 +4,36 @@ const http = require('http');
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
let port = process.argv[2] || 3000; // Set the port from the command line argument or default to 3000
|
|
8
|
+
try{
|
|
9
|
+
port = parseInt(port);
|
|
10
|
+
if (isNaN(port) || port < 1 || port > 65535) {
|
|
11
|
+
console.log('Invalid port number. Falling back to default port 3000.');
|
|
12
|
+
port = 3000;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
console.log('Invalid port number. Falling back to default port 3000.');
|
|
17
|
+
port = 3000;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let notFoundFile = null;
|
|
21
|
+
let noCache = false;
|
|
22
|
+
|
|
23
|
+
// loop through the arguments from index 3
|
|
24
|
+
for (let i = 2; i < process.argv.length; i++) {
|
|
25
|
+
if (process.argv[i].startsWith('404=')) {
|
|
26
|
+
notFoundFile = notFoundFile.split('=').slice(1).join('=');
|
|
27
|
+
}
|
|
28
|
+
else if (process.argv[i] === 'no-cache=true') {
|
|
29
|
+
noCache = true;
|
|
30
|
+
}
|
|
31
|
+
// else if it's a file name
|
|
32
|
+
else if (process.argv[i].endsWith('.html') || process.argv[i].endsWith('.htm') || process.argv[i].endsWith('.txt')) {
|
|
33
|
+
notFoundFile = process.argv[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
9
37
|
const mimeJson = path.join(__dirname, '/mime.json');
|
|
10
38
|
|
|
11
39
|
const getContentType = (() => {
|
|
@@ -21,6 +49,13 @@ const getContentType = (() => {
|
|
|
21
49
|
})
|
|
22
50
|
})();
|
|
23
51
|
|
|
52
|
+
function setNoCache(res) {
|
|
53
|
+
res.setNoCache('Cache-Control', 'no-cache, no-store, must-revalidate');
|
|
54
|
+
res.setNoCache('Pragma', 'no-cache');
|
|
55
|
+
res.setNoCache('Expires', '0');
|
|
56
|
+
return res;
|
|
57
|
+
}
|
|
58
|
+
|
|
24
59
|
const server = http.createServer((req, res) => {
|
|
25
60
|
// Extract the file path from the request URL
|
|
26
61
|
let filePath = path.join(process.cwd(), req.url);
|
|
@@ -94,6 +129,10 @@ const server = http.createServer((req, res) => {
|
|
|
94
129
|
} else {
|
|
95
130
|
// Send the file contents as the response
|
|
96
131
|
res.statusCode = 200;
|
|
132
|
+
// set no-cache header if noCache is true
|
|
133
|
+
if (noCache) {
|
|
134
|
+
setNoCache(res);
|
|
135
|
+
}
|
|
97
136
|
res.end(data);
|
|
98
137
|
}
|
|
99
138
|
});
|
|
@@ -104,4 +143,11 @@ const server = http.createServer((req, res) => {
|
|
|
104
143
|
|
|
105
144
|
server.listen(port, () => {
|
|
106
145
|
console.log(`Server running on port ${port}`);
|
|
146
|
+
console.log(`404 file: ${notFoundFile}`);
|
|
147
|
+
console.log(`No cache: ${noCache}`);
|
|
148
|
+
console.log(`Serving files from: ${process.cwd()}`);
|
|
149
|
+
console.log(`Use Ctrl+C to stop the server`);
|
|
150
|
+
console.log(`Use 'bns.js <port> 404=<file>' to set a custom 404 file`);
|
|
151
|
+
console.log(`Use 'bns.js <port> no-cache=true' to set no-cache headers`);
|
|
152
|
+
console.log(`Use 'bns.js <port> 404=<file> no-cache=true' to set a custom 404 file and no-cache headers`);
|
|
107
153
|
});
|
package/index.html
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
<h2>How to use</h2>
|
|
14
14
|
<pre>
|
|
15
|
-
node bns [port] [404 file]
|
|
15
|
+
node bns [port] 404=[404 file] no-cache=[true]
|
|
16
16
|
</pre>
|
|
17
17
|
<p>
|
|
18
18
|
The [port] argument is optional. If no port is given, it will default to 3000.
|
|
@@ -23,12 +23,20 @@ node bns [port] [404 file]
|
|
|
23
23
|
node bns 8080
|
|
24
24
|
</pre>
|
|
25
25
|
<p>
|
|
26
|
-
The [404 file] argument is optional. If file location is given, it will respond the file on 404.
|
|
26
|
+
The 404=[404 file] argument is optional. If file location is given, it will respond the file on 404.
|
|
27
27
|
<br>
|
|
28
28
|
For example, if you want your 404 page to be 404.html on port 8080, you would run:
|
|
29
29
|
</p>
|
|
30
30
|
<pre>
|
|
31
|
-
node bns 8080 404.html
|
|
31
|
+
node bns 8080 404=404.html
|
|
32
|
+
</pre>
|
|
33
|
+
<p>
|
|
34
|
+
The no-cache=[true] argument is optional. If no-cache option is given, it will respond with no-cache headers.
|
|
35
|
+
<br>
|
|
36
|
+
For example, if you want to prevent your page from being cached, you would run:
|
|
37
|
+
</p>
|
|
38
|
+
<pre>
|
|
39
|
+
node bns no-cache=true
|
|
32
40
|
</pre>
|
|
33
41
|
<h2>NPM</h2>
|
|
34
42
|
<pre>
|
|
@@ -38,5 +46,5 @@ npm install basic-node-server
|
|
|
38
46
|
Then, you can run it from your project directory:
|
|
39
47
|
</p>
|
|
40
48
|
<pre>
|
|
41
|
-
npx bns [port] [404 file]
|
|
49
|
+
npx bns [port] 404=[404 file] no-cache=[true]
|
|
42
50
|
</pre>
|