Skip to content

Commit d4a5976

Browse files
committed
c-common revision
1 parent b07c96c commit d4a5976

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

JavaScript/c-commonjs/static.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,34 @@ const http = require('node:http');
44
const path = require('node:path');
55
const fs = require('node:fs');
66

7+
const MIME_TYPES = {
8+
html: 'text/html; charset=UTF-8',
9+
json: 'application/json; charset=UTF-8',
10+
js: 'application/javascript; charset=UTF-8',
11+
css: 'text/css',
12+
png: 'image/png',
13+
ico: 'image/x-icon',
14+
svg: 'image/svg+xml',
15+
};
16+
17+
const HEADERS = {
18+
'X-XSS-Protection': '1; mode=block',
19+
'X-Content-Type-Options': 'nosniff',
20+
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains; preload',
21+
'Access-Control-Allow-Origin': '*',
22+
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
23+
'Access-Control-Allow-Headers': 'Content-Type',
24+
};
25+
726
module.exports = (root, port, console) => {
827
http.createServer(async (req, res) => {
928
const url = req.url === '/' ? '/index.html' : req.url;
1029
const filePath = path.join(root, url);
1130
try {
1231
const data = await fs.promises.readFile(filePath);
32+
const fileExt = path.extname(filePath).substring(1);
33+
const mimeType = MIME_TYPES[fileExt] || MIME_TYPES.html;
34+
res.writeHead(200, { ...HEADERS, 'Content-Type': mimeType });
1335
res.end(data);
1436
} catch (err) {
1537
res.statusCode = 404;

JavaScript/c-commonjs/static/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ transport.http = (url) => (structure) => {
1111
const methods = Object.keys(service);
1212
for (const method of methods) {
1313
api[name][method] = (...args) => new Promise((resolve, reject) => {
14-
fetch(`${url}/${name}/${method}/${args}`, {
14+
fetch(`${url}/api/${name}/${method}`, {
1515
method: 'POST',
1616
headers: { 'Content-Type': 'application/json' },
1717
body: JSON.stringify({ args }),
@@ -72,6 +72,6 @@ const scaffold = (url) => {
7272
say: ['message'],
7373
}
7474
});
75-
const data = await api.talks.say('hello');
75+
const data = await api.user.read();
7676
console.log({ data });
7777
})();

JavaScript/c-commonjs/transport/http.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ const receiveArgs = async (req) => {
2020
};
2121

2222
module.exports = (routing, port, console) => {
23-
http.createServer(async (req, res) => {
24-
res.writeHead(200, HEADERS);
25-
const { url, socket } = req;
26-
const [name, method, id] = url.substring(1).split('/');
27-
const entity = routing[name];
28-
if (!entity) return void res.end('"Not found"');
29-
const handler = entity[method];
30-
if (!handler) return void res.end('"Not found"');
31-
const src = handler.toString();
32-
const signature = src.substring(0, src.indexOf(')'));
33-
const args = [];
34-
if (id) args.push(id);
35-
if (signature.includes('{')) args.push(await receiveArgs(req));
36-
const result = await handler(...args);
37-
console.log(`${socket.remoteAddress} ${method} ${url}`);
38-
res.end(JSON.stringify(result.rows ? result.rows : result));
39-
}).listen(port);
23+
http
24+
.createServer(async (req, res) => {
25+
res.writeHead(200, HEADERS);
26+
if (req.method !== 'POST') return void res.end('"Not found"');
27+
const { url, socket } = req;
28+
const [place, name, method] = url.substring(1).split('/');
29+
if (place !== 'api') return void res.end('"Not found"');
30+
const entity = routing[name];
31+
if (!entity) return void res.end('"Not found"');
32+
const handler = entity[method];
33+
if (!handler) return void res.end('"Not found"');
34+
const { args } = await receiveArgs(req);
35+
console.log(`${socket.remoteAddress} ${method} ${url}`);
36+
const result = await handler(...args);
37+
res.end(JSON.stringify(result));
38+
})
39+
.listen(port);
4040

4141
console.log(`API on port ${port}`);
4242
};

JavaScript/d-messenger/static/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ transport.http = (url) => (structure) => {
1515
fetch(`${url}/api/${name}/${method}`, {
1616
method: 'POST',
1717
headers: { 'Content-Type': 'application/json' },
18-
body: JSON.stringify({ ...args }),
18+
body: JSON.stringify({ args }),
1919
}).then((res) => {
2020
if (res.status === 200) resolve(res.json());
2121
else reject(new Error(`Status Code: ${res.status}`));

JavaScript/d-messenger/transport/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = (routing, port, console) => {
3333
if (!handler) return void res.end('"Not found"');
3434
const { args } = await receiveArgs(req);
3535
console.log(`${socket.remoteAddress} ${method} ${url}`);
36-
const result = await handler(args);
36+
const result = await handler(...args);
3737
res.end(JSON.stringify(result));
3838
})
3939
.listen(port);

0 commit comments

Comments
 (0)