-
Notifications
You must be signed in to change notification settings - Fork 3
/
local.js
115 lines (113 loc) · 3.95 KB
/
local.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
const http = require('http');
const fs = require('fs');
const path = require('path');
const config = require('./config.json');
const { connector, hosting } = require('./src/connector');
const hosted_files = {};
const envParams = {
get: (key) => config[key]
};
const hostingParams = {
put: async (title, readableStream, data) => {
const httpEtag = new Date().getTime().toString();
console.log('PUT: ' + httpEtag);
hosted_files[httpEtag] = {
Body: readableStream,
Metadata: data
};
return { httpEtag };
},
get: async (httpEtag) => {
console.log('GET: ' + httpEtag);
if (hosted_files[httpEtag]) {
return {
body: hosted_files[httpEtag].Body,
writeHttpMetadata(headers) {
headers['Content-Type'] = hosted_files[httpEtag].Metadata['Content-Type'];
headers['Content-Length'] = hosted_files[httpEtag].Metadata['Content-Length'];
}
};
} else {
console.log('etag_not_found:', hosted_files);
}
},
head: async (httpEtag) => {
console.log('HEAD: ' + httpEtag);
return hosted_files.hasOwnProperty(httpEtag);
}
};
const server = http.createServer(async (req, res) => {
const apiRequest = {
method: req.method,
json() {
return new Promise((resolve, reject) => {
let body = '';
req.on('data', (chunk) => body += chunk);
req.on('end', () => {
try {
resolve(JSON.parse(body));
}
catch (err) {
reject(err);
}
});
});
}
};
const readFile = (filePath, fileType) => new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
res.statusCode = 500;
res.end('Internal Server Error');
} else {
res.setHeader('Content-Type', fileType);
res.end(data);
}
resolve();
});
});
if (req.url === '/') {
const indexPath = path.join(__dirname, 'src', 'app.html');
await readFile(indexPath, 'text/html');
} else if (req.url === '/nes.min.css') {
const indexPath = path.join(__dirname, 'src', 'nes.min.css');
await readFile(indexPath, 'text/css');
} else if (req.url === '/app.css') {
const indexPath = path.join(__dirname, 'src', 'app.css');
await readFile(indexPath, 'text/css');
} else if (req.url === '/app.js') {
const indexPath = path.join(__dirname, 'src', 'app.js');
await readFile(indexPath, 'text/javascript');
} else if (req.url.startsWith('/shared/')) {
const response = await hosting({ ...req, url: req.url.slice(8) }, hostingParams);
if (response.status !== 200) {
res.writeHead(response.status, { 'Content-Type': 'text/plain' });
res.end(response.body);
return;
}
for (let key in response.headers) {
res.setHeader(key, response.headers[key]);
}
res.end(response.body);
} else if (req.url === '/api') {
const response = await connector(apiRequest, envParams, hostingParams);
if (response.status !== 200) {
res.writeHead(response.status, { 'Content-Type': 'text/plain' });
res.end(response.body);
return;
}
for (let key in response.headers) {
res.setHeader(key, response.headers[key]);
}
res.end(response.body);
} else {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request');
}
});
server.listen(3000, () => {
console.log('Server is running: http://localhost:3000');
});
server.on('error', (err) => {
console.error('Server error:', err.message);
});