-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_server.js
79 lines (67 loc) · 2.04 KB
/
http_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
var fs = require('fs');
var http = require('http');
var url = require('url');
const { db } = require('./api/db');
var ROOT_DIR = "./dist/";
var mimeTypes ={
"js":"text/javascript",
"json":"text/data",
"html":"text/html",
"png":"image/png",
"jpg":"image/jpg",
"jpeg":"image/jpeg"
};
let app = { };
db(app);
let routes = [... fs.readdirSync('./api/routes')]
.filter(x => !['example.js'].includes(x) && x.endsWith('.js'))
.map(x => [x, require(`./api/routes/${x}`)])
.map(x => ({ name: x[0].replace('.js', ''), use: Object.values(x[1])[0], type: 'normal' }));
[... routes].forEach(routeUseFunction => {
routeUseFunction.use(app);
let method = routeUseFunction.name.split('-', 1)[0];
let path = routeUseFunction.name.replace(`${method}-`, '');
console.log(`[Explorer - API] - ${method} - ${path}`);
});
const headers = {
'Access-Control-Allow-Origin': '*', /* @dev First, read about security */
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
'Access-Control-Max-Age': 2592000, // 30 days
/** add other headers as per requirement */
};
http.createServer( function( req, res ) {
if (req.method === 'OPTIONS') {
res.writeHead(204, headers);
res.end();
return;
}
var urlObj = url.parse( req.url, true, false );
var tmp = urlObj.pathname.lastIndexOf(".");
var extension = urlObj.pathname.substring((tmp + 1));
let filePath = urlObj.pathname.replace('..', '');
if (app[filePath.replace(/^\//, '')] != undefined) {
app[filePath.replace(/^\//, '')](req, res, headers);
return ;
}
if (filePath == undefined || filePath == '' || filePath == '/') {
filePath = 'index.html';
}
if (!fs.existsSync(ROOT_DIR + filePath)) {
filePath = 'index.html';
}
fs.readFile( ROOT_DIR + filePath, function( err, data ){
if( err ){
res.writeHead(404);
res.end(JSON.stringify( err ) );
return;
}
if( mimeTypes[ extension ] ){
res.writeHead(200, {'Content-Type': mimeTypes[extension]});
}
else {
res.writeHead(200);
}
res.end(data);
} )
}).listen( 8080, '0.0.0.0' );
console.log( "listening on port 0.0.0.0:8080" );