Skip to content

Commit 5238b0f

Browse files
committed
Improve code style
1 parent 342b756 commit 5238b0f

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

JavaScript/1-HTTP/server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const httpError = (res, status, message) => {
5757
res.end(`"${message}"`);
5858
};
5959

60-
http.createServer(async (req, res) => {
60+
const controller = async (req, res) => {
6161
const url = req.url === '/' ? '/index.html' : req.url;
6262
const [first, second] = url.substring(1).split('/');
6363
if (first === 'api') {
@@ -83,4 +83,6 @@ http.createServer(async (req, res) => {
8383
httpError(res, 404, 'File is not found');
8484
}
8585
}
86-
}).listen(8000);
86+
};
87+
88+
http.createServer(controller).listen(8000);

JavaScript/1-HTTP/static/client.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
const buildAPI = (methods) => {
44
const api = {};
55
for (const method of methods) {
6-
api[method] = (...args) => new Promise((resolve, reject) => {
7-
const url = `/api/${method}`;
8-
console.log(url, args);
9-
fetch(url, {
10-
method: 'POST',
11-
headers: { 'Content-Type': 'application/json' },
12-
body: JSON.stringify(args),
13-
}).then((res) => {
14-
const { status } = res;
15-
if (status !== 200) {
16-
reject(new Error(`Status Code: ${status}`));
17-
return;
18-
}
19-
resolve(res.json());
6+
api[method] = (...args) =>
7+
new Promise((resolve, reject) => {
8+
const url = `/api/${method}`;
9+
console.log(url, args);
10+
fetch(url, {
11+
method: 'POST',
12+
headers: { 'Content-Type': 'application/json' },
13+
body: JSON.stringify(args),
14+
}).then((res) => {
15+
const { status } = res;
16+
if (status !== 200) {
17+
reject(new Error(`Status Code: ${status}`));
18+
return;
19+
}
20+
resolve(res.json());
21+
});
2022
});
21-
});
2223
}
2324
return api;
2425
};

JavaScript/2-WS/server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ setTimeout(() => {
4646
console.dir({ api });
4747
}, 1000);
4848

49-
const server = http.createServer(async (req, res) => {
49+
const controller = async (req, res) => {
5050
const url = req.url === '/' ? '/index.html' : req.url;
5151
const [file] = url.substring(1).split('/');
5252
const path = `./static/${file}`;
@@ -57,7 +57,9 @@ const server = http.createServer(async (req, res) => {
5757
res.statusCode = 404;
5858
res.end('"File is not found"');
5959
}
60-
}).listen(8000);
60+
};
61+
62+
const server = http.createServer(controller).listen(8000);
6163

6264
const ws = new WebSocket.Server({ server });
6365

JavaScript/2-WS/static/client.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ const socket = new WebSocket('ws://127.0.0.1:8000/');
55
const buildAPI = (methods) => {
66
const api = {};
77
for (const method of methods) {
8-
api[method] = (...args) => new Promise((resolve) => {
9-
socket.send(JSON.stringify({ method, args }));
10-
socket.onmessage = (event) => {
11-
const data = JSON.parse(event.data);
12-
resolve(data);
13-
};
14-
});
8+
api[method] = (...args) =>
9+
new Promise((resolve) => {
10+
socket.send(JSON.stringify({ method, args }));
11+
socket.onmessage = (event) => {
12+
const data = JSON.parse(event.data);
13+
resolve(data);
14+
};
15+
});
1516
}
1617
return api;
1718
};

0 commit comments

Comments
 (0)