@rexaray008/nodeproxy 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/server.js CHANGED
@@ -110,21 +110,23 @@ function proxyRequest(target, req, res, config) {
110
110
  req.pipe(proxyReq, { end: true });
111
111
  }
112
112
 
113
- function makeRequestHandler(config) {
113
+ // "listener" is one entry from config.servers[] — it carries its own
114
+ // routes, so each port is fully independent of the others.
115
+ function makeRequestHandler(listener, globalConfig) {
114
116
  return function requestHandler(req, res) {
115
- const route = matchRoute(config.routes, req.headers.host);
117
+ const route = matchRoute(listener.routes, req.headers.host);
116
118
 
117
- log(config, req.method, req.headers.host || '-', req.url, '->', route ? route.host : 'no-match');
119
+ log(globalConfig, `[:${listener.port}]`, req.method, req.headers.host || '-', req.url, '->', route ? route.host : 'no-match');
118
120
 
119
121
  if (!route) {
120
122
  res.writeHead(404, { 'Content-Type': 'text/plain' });
121
- return res.end('404 Not Found (no matching route for this host)');
123
+ return res.end('404 Not Found (no matching route for this host on this port)');
122
124
  }
123
125
 
124
126
  const target = pickTarget(route);
125
127
 
126
128
  if (target) {
127
- return proxyRequest(target, req, res, config);
129
+ return proxyRequest(target, req, res, globalConfig);
128
130
  }
129
131
 
130
132
  if (route.staticDir) {
@@ -136,32 +138,43 @@ function makeRequestHandler(config) {
136
138
  };
137
139
  }
138
140
 
139
- function startServer(config) {
140
- const handler = makeRequestHandler(config);
141
- const servers = [];
141
+ // Starts one listener (HTTP, and HTTPS too if enabled+configured for it).
142
+ function startListener(listener, globalConfig) {
143
+ const handler = makeRequestHandler(listener, globalConfig);
144
+ const started = [];
142
145
 
143
- const httpServer = http.createServer(handler);
144
- httpServer.listen(config.port, () => {
145
- console.log(`[nodeproxy] HTTP listening on port ${config.port}`);
146
- });
147
- servers.push(httpServer);
148
-
149
- if (config.https && config.https.enabled) {
150
- if (!config.https.key || !config.https.cert) {
151
- console.warn('[nodeproxy] https.enabled is true but key/cert paths are missing — skipping HTTPS.');
146
+ if (listener.https && listener.https.enabled) {
147
+ if (!listener.https.key || !listener.https.cert) {
148
+ console.warn(`[nodeproxy] port ${listener.port}: https.enabled is true but key/cert paths are missing — starting as plain HTTP instead.`);
152
149
  } else {
153
150
  const httpsOptions = {
154
- key: fs.readFileSync(config.https.key),
155
- cert: fs.readFileSync(config.https.cert)
151
+ key: fs.readFileSync(listener.https.key),
152
+ cert: fs.readFileSync(listener.https.cert)
156
153
  };
157
154
  const httpsServer = https.createServer(httpsOptions, handler);
158
- httpsServer.listen(config.https.port, () => {
159
- console.log(`[nodeproxy] HTTPS listening on port ${config.https.port}`);
155
+ httpsServer.listen(listener.port, () => {
156
+ console.log(`[nodeproxy] HTTPS listening on port ${listener.port}`);
160
157
  });
161
- servers.push(httpsServer);
158
+ started.push(httpsServer);
159
+ return started;
162
160
  }
163
161
  }
164
162
 
163
+ const httpServer = http.createServer(handler);
164
+ httpServer.listen(listener.port, () => {
165
+ console.log(`[nodeproxy] HTTP listening on port ${listener.port}`);
166
+ });
167
+ started.push(httpServer);
168
+ return started;
169
+ }
170
+
171
+ // Starts every listener defined in config.servers[]. Each one is fully
172
+ // independent — different port, different routes, different projects.
173
+ function startServer(config) {
174
+ let servers = [];
175
+ for (const listener of config.servers) {
176
+ servers = servers.concat(startListener(listener, config));
177
+ }
165
178
  return servers;
166
179
  }
167
180
 
@@ -1,26 +1,37 @@
1
1
  {
2
- "port": 8080,
3
- "https": {
4
- "enabled": false,
5
- "port": 8443,
6
- "key": "",
7
- "cert": ""
8
- },
9
2
  "logging": true,
10
- "routes": [
3
+ "apps": [
11
4
  {
12
- "host": "*",
13
- "targets": [
14
- "http://localhost:3000"
15
- ],
16
- "staticDir": null
17
- },
5
+ "name": "appA",
6
+ "cmd": "npm start",
7
+ "cwd": "/home/hrn/Desktop/www/rayUi/ray-ui-kit"
8
+ }
9
+ ],
10
+ "servers": [
18
11
  {
19
- "host": "https://hirenray.rest",
20
- "targets": [
21
- "http://localhost:3000"
22
- ],
23
- "staticDir": null
12
+ "port": 8080,
13
+ "https": {
14
+ "enabled": false,
15
+ "port": 8443,
16
+ "key": "",
17
+ "cert": ""
18
+ },
19
+ "routes": [
20
+ {
21
+ "host": "*",
22
+ "targets": [
23
+ "http://localhost:3000"
24
+ ],
25
+ "staticDir": null
26
+ },
27
+ {
28
+ "host": "https://hirenray.rest",
29
+ "targets": [
30
+ "http://localhost:3000"
31
+ ],
32
+ "staticDir": null
33
+ }
34
+ ]
24
35
  }
25
36
  ]
26
37
  }
package/package.json CHANGED
@@ -1,13 +1,10 @@
1
1
  {
2
2
  "name": "@rexaray008/nodeproxy",
3
- "publishConfig": {
4
- "access": "public"
5
- },
6
- "version": "1.0.1",
7
- "description": "A lightweight, zero-dependency reverse proxy / web server (like a mini nginx) for Node.js. One command to install, configure and run.",
3
+ "version": "2.0.0",
4
+ "description": "A lightweight, zero-dependency reverse proxy / web server for Node.js, with pm2 app management and Cloudflare Tunnel integration to expose local projects on a real domain. One command to install, configure and run.",
8
5
  "main": "lib/server.js",
9
6
  "bin": {
10
- "nodeproxy": "bin/cli.js"
7
+ "nodeproxy": "./bin/cli.js"
11
8
  },
12
9
  "scripts": {
13
10
  "start": "node bin/cli.js start"