-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
95 lines (94 loc) · 3.41 KB
/
server.mjs
File metadata and controls
95 lines (94 loc) · 3.41 KB
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
import express from 'express';
import http from 'http';
import fs from 'fs';
import url from 'url';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
//------------------------------------------------------------
var $vm={};
//------------------------------------------------------------
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
//const web_root = path.join(__dirname, './');
//console.log(__dirname)
//----------------------------------------------------------
$vm.request_get=function(req,res){
const parsedUrl = url.parse(req.url, true);
let pathName = parsedUrl.pathname;
pathName=pathName.replace(/%20/g,' ');
pathName=pathName.replace(/\.\./g,' ');
const file = path.join(__dirname, pathName);
//------------------------------------------------------------
const responseContentType = getContentType(file);
res.setHeader('Content-Type', responseContentType);
console.log(file)
fs.readFile(file, (error, data) => {
if (!error) {
res.writeHead(200);
res.end(data);
} else {
res.writeHead(404);
res.end('404 - File Not Found');
}
});
}
//----------------------------------------------------------
const mimeTypes = {
'.html': 'text/html',
'.jpg': 'image/jpeg',
'.css': 'text/css',
'.js': 'text/javascript',
'.png': 'image/png',
'.ico': 'image/x-icon',
};
//------------------------------------------------------------
const getContentType = pathName => {
let contentType = 'application/octet-stream';
for (var key in mimeTypes) {
if (mimeTypes.hasOwnProperty(key)) {
if (pathName.indexOf(key) > -1) {
contentType = mimeTypes[key];
}
}
}
return contentType;
};
//------------------------------------------------------------
$vm.process=function(req, res, next){
//-------------------------------------------------
/*
res.removeHeader("X-Powered-By");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept");
*/
res.removeHeader("X-Powered-By");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Credentials", "true");
//-------------------------------------------------
var dt1=new Date().getTime();
//-------------------------------------------------
var r_data='';
req.on('data',function(data){r_data+=data;});
req.on('end',function(){
//-------------------------------------------------
if(req.method=='GET') { $vm.request_get(req,res); }
else if(req.method=='POST') { res.send(""); res.end(); }
else if(req.method=="OPTIONS") { res.writeHead(200); res.send(""); res.end(); }
else if(req.method=="HEAD") { res.send(""); res.end(); }
//-------------------------------------------------
})
}
//------------------------------------------------------------
const server=express();
server.use($vm.process);
//------------------------------------------------------------
http.createServer(
server
).listen(
8080,
() => console.log("The server is listening on port 8080")
);
//------------------------------------------------------------