Cannot call method 'split' of undefined
Problem
I'm a newbie to `node-http-proxy` module. my aim I need to use the module provide multi-SSL for multi-subdomain. For example; if a user call `process.localhost:1443` then I should route the call to `process.localhost:2443` and if a user call `api.localhost:1443` then I should route the call to `api.localhost:3443` what's happening I wrote the below server.js codes. _If I change `httpProxy.createServer(options)` line with `httpProxy.createServer({target:'http://process.localhost:2443'})` then it works properly!_ <br> Otherwise when I try to call `process.localhost:1443` I get the following error;<br> `D:\Work Space\...\http-proxy\node_modules\requires-port\index.js:13` `protocol = protocol.split(':')[0];` `TypeError: Cannot call method 'split' of undefined` `protocol` seems as `undefined`. [code block] What should I do? <br><br> server.js [code block]
Error Output
Error: Cannot call method 'split' of undefined`
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix Undefined Protocol Error in Node-HTTP-Proxy Configuration
The error occurs because the 'protocol' variable is undefined in the requires-port module. This happens when the options passed to httpProxy.createServer do not include a valid 'target' URL with a defined protocol (http or https). Without a proper target, the proxy cannot determine how to handle the request, leading to the 'split' method being called on an undefined value.
Awaiting Verification
Be the first to verify this fix
- 1
Define Target URL with Protocol
Ensure that the target URL in the options for httpProxy.createServer includes a valid protocol. This is necessary for the proxy to function correctly.
javascriptconst options = { target: 'http://process.localhost:2443' }; - 2
Implement Dynamic Target Routing
Modify the server setup to dynamically route requests based on the subdomain. Use a function to determine the target based on the incoming request's hostname.
javascriptconst httpProxy = require('http-proxy'); const proxy = httpProxy.createProxyServer(); const server = require('http').createServer((req, res) => { const host = req.headers.host; let target; if (host === 'process.localhost:1443') { target = 'http://process.localhost:2443'; } else if (host === 'api.localhost:1443') { target = 'http://api.localhost:3443'; } else { res.writeHead(404); return res.end('Not Found'); } proxy.web(req, res, { target }); }); - 3
Handle Errors Gracefully
Add error handling to the proxy server to catch any issues that may arise during the proxying process. This will help in debugging and provide a better user experience.
javascriptproxy.on('error', (err, req, res) => { console.error('Proxy error:', err); res.writeHead(500, { 'Content-Type': 'text/plain' }); res.end('Something went wrong.'); }); - 4
Start the Server
Ensure that the server is listening on the correct port for incoming requests. This is crucial for the proxy to receive and handle requests properly.
javascriptserver.listen(1443, () => { console.log('Proxy server is running on https://localhost:1443'); });
Validation
Test the server by making requests to both process.localhost:1443 and api.localhost:1443. Ensure that they are correctly routed to process.localhost:2443 and api.localhost:3443 respectively without any errors. Check the console for any proxy errors.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep