Skip to content

Commit 197e45e

Browse files
authored
refactoring
1 parent 6e274c9 commit 197e45e

File tree

5 files changed

+38
-61
lines changed

5 files changed

+38
-61
lines changed

.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v18.20.2
1+
v16.20.2

README.md

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
![proxy-lib](icon.png "proxy-lib")
1+
![node-proxy-lib](icon.png "node-proxy-lib")
22

33
# node-proxy-lib
4-
nodejs proxy server
4+
node.js proxy server
55

6-
```sh
6+
```
77
npm i node-proxy-lib
88
```
99

1010
```js
1111
import { createProxy } from 'node-proxy-lib';
1212

13-
const proxy = createProxy({
14-
host: '127.0.0.1',
15-
port: 3030
16-
});
13+
const proxy = createProxy();
1714

1815
proxy([
1916
{
20-
pattern: /\/ssr\/.*/,
17+
pattern: /(?<path>api\/.+)/,
2118
host: '127.0.0.1',
22-
port: 8080
19+
port: 8081
2320
},
2421
{
25-
pattern: /\/gateway\/.+/,
22+
pattern: /(?<path>.+)/,
2623
host: '127.0.0.1',
27-
port: 8081
24+
port: 8080
2825
}
2926
]);
3027
```

index.mjs

+23-25
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { createServer, request } from 'node:http';
22

3-
const getBody = incomingMessage => {
3+
const getBody = (incomingMessage, encoding='utf-8') => {
44
return new Promise(resolve => {
5-
let body = '';
5+
let body = [];
66
incomingMessage.on('data', chunk => {
7-
body += chunk;
7+
body.push(chunk);
88
});
99
incomingMessage.on('end', () => {
10-
resolve(body);
10+
const buffer = Buffer.concat(body);
11+
const content = buffer.toString(encoding);
12+
resolve(content);
1113
});
1214
});
1315
};
@@ -17,34 +19,33 @@ export const createProxy = options => proxyOptions => {
1719
const port = options?.port || 3030;
1820
const host = options?.host || '127.0.0.1';
1921
const server = createServer(async (incomingMessage, serverResponse) => {
20-
const path = incomingMessage.url;
22+
const url = incomingMessage.url;
2123
const option = proxyOptions.find(option => {
22-
return option.pattern.test(path)
24+
return option.pattern.test(url)
2325
});
26+
const encoding = 'utf8';
2427
if (!option) {
2528
serverResponse.statusCode = 404;
26-
serverResponse.end('NO ROUTE');
29+
serverResponse.end('NO ROUTE', encoding);
2730
return;
2831
}
32+
const match = url.match(option.pattern);
33+
const groups = match?.groups;
34+
const path = groups?.path;
2935
console.table('__INCOMING_MESSAGE__');
3036
console.table([
3137
{
3238
protocol,
3339
host,
3440
port,
35-
path,
3641
method: incomingMessage.method,
37-
pattern: option.pattern
3842
}
3943
]);
44+
console.table({
45+
path
46+
});
4047
console.group();
4148
const body = await getBody(incomingMessage);
42-
const content = JSON.parse(body || '{}');
43-
console.log('__INCOMING_MESSAGE_BODY__');
44-
console.table([
45-
content
46-
]);
47-
4849
const clientRequest = request({
4950
host: option.host,
5051
port: option.port,
@@ -55,26 +56,23 @@ export const createProxy = options => proxyOptions => {
5556
}, async response => {
5657
console.group();
5758
const body = await getBody(response);
58-
const content = JSON.parse(body || '{}');
5959
console.log('__SERVER_RESPONSE__');
6060
console.table([
6161
{
6262
protocol,
6363
host: option.host,
6464
port: option.port,
65-
path,
6665
method: incomingMessage.method,
67-
pattern: option.pattern
66+
status: response.statusCode
6867
}
6968
]);
70-
console.log('__SERVER_RESPONSE_BODY__');
71-
console.table([
72-
content
73-
]);
74-
serverResponse.end(body);
69+
console.table({
70+
path
71+
});
72+
serverResponse.end(body, encoding);
7573
console.groupEnd();
7674
});
77-
clientRequest.end(body);
75+
clientRequest.end(body, encoding);
7876
console.groupEnd();
7977
});
8078

@@ -106,7 +104,7 @@ export const createProxy = options => proxyOptions => {
106104
close();
107105
});
108106

109-
server.on('clientError', error => {
107+
server.on('clientError', (error, socket) => {
110108
console.log(error);
111109
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
112110
});

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
2-
"name": "proxy-lib",
2+
"name": "node-proxy-lib",
33
"version": "1.0.0",
4-
"description": "Node.js proxy",
5-
"keywords": ["node", "proxy", "native", "one file"],
4+
"license": "MIT",
5+
"description": "Node.js proxy server",
6+
"keywords": ["node", "proxy", "server"],
67
"repository": {
78
"type": "git",
8-
"url": "https://github.com/AirStair/proxy-lib"
9+
"url": "https://github.com/AirStair/node-proxy-lib"
910
},
1011
"author": {
1112
"name": "AirStair",
@@ -15,6 +16,6 @@
1516
"main": "index.mjs",
1617
"type": "module",
1718
"engines": {
18-
"node": "18.20.2"
19+
"node": "16.20.2"
1920
}
2021
}

proxy.mjs

-19
This file was deleted.

0 commit comments

Comments
 (0)