basic-node-server 1.0.0
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/LICENSE +21 -0
- package/README.md +17 -0
- package/application.csv +1578 -0
- package/index.html +1 -0
- package/index.js +69 -0
- package/package.json +18 -0
package/index.html
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<h1>Simple node server</h1>
|
package/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const http = require('http');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const port = process.argv[2] || 3000; // Set the port from the command line argument or default to 3000
|
|
6
|
+
|
|
7
|
+
const csvFilePath = path.join(__dirname, '/application.csv');
|
|
8
|
+
|
|
9
|
+
const getContentType = (() => {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
fs.readFile(csvFilePath, 'utf8', (err, csvData) => {
|
|
12
|
+
if (err) {
|
|
13
|
+
reject(err);
|
|
14
|
+
} else {
|
|
15
|
+
const contentTypeMap = csvData.split('\n')
|
|
16
|
+
.map(row => row.split(','))
|
|
17
|
+
.reduce((map, [extension, contentType]) => {
|
|
18
|
+
map[extension] = contentType;
|
|
19
|
+
return map;
|
|
20
|
+
}, {});
|
|
21
|
+
|
|
22
|
+
resolve(contentTypeMap);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
})
|
|
26
|
+
})();
|
|
27
|
+
|
|
28
|
+
const server = http.createServer((req, res) => {
|
|
29
|
+
// Extract the file path from the request URL
|
|
30
|
+
let filePath = path.join(process.cwd(), req.url);
|
|
31
|
+
|
|
32
|
+
// If filePath is empty or ends with '/', default to 'index.html'
|
|
33
|
+
if (!filePath || filePath.endsWith('/')) {
|
|
34
|
+
filePath = path.join(filePath, 'index.html');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Check if the file exists
|
|
38
|
+
fs.access(filePath, fs.constants.F_OK, (err) => {
|
|
39
|
+
if (err) {
|
|
40
|
+
// File not found
|
|
41
|
+
res.statusCode = 404;
|
|
42
|
+
res.end('File not found');
|
|
43
|
+
} else {
|
|
44
|
+
// Read the file and send it as the response
|
|
45
|
+
const fileExtension = path.extname(filePath).substring(1);
|
|
46
|
+
getContentType.then(contentType => {
|
|
47
|
+
contentType[fileExtension] || 'application/octet-stream';
|
|
48
|
+
|
|
49
|
+
res.setHeader('Content-Type', contentType);
|
|
50
|
+
|
|
51
|
+
fs.readFile(filePath, (err, data) => {
|
|
52
|
+
if (err) {
|
|
53
|
+
// Error reading the file
|
|
54
|
+
res.statusCode = 500;
|
|
55
|
+
res.end('Internal server error');
|
|
56
|
+
} else {
|
|
57
|
+
// Send the file contents as the response
|
|
58
|
+
res.statusCode = 200;
|
|
59
|
+
res.end(data);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
server.listen(port, () => {
|
|
68
|
+
console.log(`Server running on port ${port}`);
|
|
69
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "basic-node-server",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simple node server for everyone.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"bns": "node index.js"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/broadwayinc/basic-node-server.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/broadwayinc/basic-node-server/issues"
|
|
15
|
+
},
|
|
16
|
+
"author": "Baksa Gimm",
|
|
17
|
+
"license": "MIT"
|
|
18
|
+
}
|