-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
executable file
·122 lines (112 loc) · 3.74 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Simple Node Web Server for sending static files using using only
* Node.js built-in features without any outside dependencies.
*
* To run:
* npm start
*
* If you do not have node/npm installed and still want to run this
* site locally then you can also run it if you have Python installed.
* Depending on the version Python installed and the OS one of the
* following commands should work:
* python3 -m http.server
* python -m http.server
* python -m SimpleHTTPServer
*
* Important - This script is very minimal by design and includes no error handling.
* It works well but has few features so is not intended for production apps.
*
* Since this file is small you can step through the code using a debugger
* which makes it is useful for learning purposes.
*
* @link https://github.com/dataformsjs/awesome-web-react/blob/master/server.js
* @author Conrad Sollitt (https://conradsollitt.com)
* @license CC0 "Public Domain" license
*/
/* Validates online with both [jshint] and [eslint] */
/* jshint esversion:8, node:true */
/* eslint-env node, es6 */
'use strict';
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
/**
* Allowed mime types
*/
const mimeTypes = {
htm: 'text/html; charset=UTF-8',
html: 'text/html; charset=UTF-8',
css: 'text/css',
txt: 'text/plain',
js: 'application/javascript',
jsx: 'application/jsx',
json: 'application/json',
graphql: 'application/graphql',
png: 'image/png',
jpg: 'image/jpeg',
svg: 'image/svg+xml',
ico: 'image/x-icon',
};
/**
* Simple Web Server for sending Static Files
*/
const server = http.createServer((req, res) => {
// Send a 404 Page as an HTML Response
function send404() {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.end('<h1>File not found</h1>');
}
// Send a Response from a File. If the file doesn't exist or the file type is
// not defined in mimeTypes then a 404 HTML Response will be sent to the client.
function sendFile(filePath) {
const data = filePath.split('.');
const fileType = data[data.length-1];
if (mimeTypes[fileType] === undefined) {
send404(); // File type not supported
return;
}
fs.readFile(filePath, (err, content) => {
if (err && err.code === 'ENOENT') {
send404(); // File not found
} else if (err) {
console.error(err);
res.statusCode = 500;
res.end('<h1>An error has occurred, check console</h1>');
} else {
res.setHeader('Content-Type', mimeTypes[fileType]);
res.end(content);
}
});
}
// Only allow GET Requests
if (req.method !== 'GET') {
res.statusCode = 405;
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.end('<h1>Method not allowed</h1>');
return;
}
// Send root [index.html] file
const reqPath = url.parse(req.url).pathname;
if (reqPath === '/') {
sendFile(path.join(__dirname, 'index.html'));
return;
}
// Does the request path match a under the scripts directory? If not send a 404.
// A security check against Path Traversal Attacks in first performed in case
// this script ends up being used on a production server.
if (reqPath.includes('../') || reqPath.includes('..\\')) {
send404();
return;
}
const filePath = path.join(__dirname, reqPath);
sendFile(filePath);
});
/**
* Start the Server
*/
const port = 3000;
server.listen(port, null, () => {
console.log(`Server running at http://127.0.0.1:${port}/`);
});