basic-node-server 1.0.11 → 1.0.13

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 CHANGED
@@ -2,13 +2,12 @@
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 root directory.
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
 
9
9
  ```
10
10
  npm install basic-node-server
11
-
12
11
  ```
13
12
 
14
13
  ## Usage
@@ -16,7 +15,7 @@ npm install basic-node-server
16
15
  Then, you can run it from your project directory:
17
16
 
18
17
  ```
19
- npx bns [port]
18
+ npx bns [port] [404.page]
20
19
  ```
21
20
 
22
21
  The [port] argument is optional. If no port is given, it will default to 3000.
@@ -25,4 +24,10 @@ For example, if you want to serve the current directory on port 8080, you would
25
24
 
26
25
  ```
27
26
  npx bns 8080
27
+ ```
28
+
29
+ If you need to setup additional 404 page you can do so by:
30
+
31
+ ```
32
+ npx bns 8080 your404page.html
28
33
  ```
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
- res.statusCode = 404;
47
- res.end('File not found');
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
- node bns [port]
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
- node bns 8080
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
- npm install basic-node-server
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
- node node_modules/basic-node-server/bns [port]
41
+ npx bns [port] [404 file]
35
42
  </pre>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "basic-node-server",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "Basic node server for everyone.",
5
5
  "main": "bns.js",
6
6
  "scripts": {