node-run-middleware icon indicating copy to clipboard operation
node-run-middleware copied to clipboard

ERROR TypeError: Cannot read property 'remoteAddress' of undefined

Open medisoft opened this issue 6 years ago • 3 comments

Hello. I'm testing this module to be able to have single api called by socket and by http, but I'm getting this error:

ERROR TypeError: Cannot read property 'remoteAddress' of undefined at forwarded (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/forwarded/index.js:31:35) at alladdrs (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/proxy-addr/index.js:57:15) at proxyaddr (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/proxy-addr/index.js:231:15) at IncomingMessage.ip (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/request.js:351:10) at getip (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/morgan/index.js:466:14) at logger (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/morgan/index.js:107:26) at Layer.handle [as handle_request] (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/router/index.js:317:13) at /Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/router/index.js:335:12) at next (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/router/index.js:275:10) at app.use (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/run-middleware/index.js:16:5) at Layer.handle [as handle_request] (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/router/index.js:317:13) at /Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/Users/osoverflow/IdeaProjects/domitai/dokku/node_modules/express/lib/router/index.js:335:12) +6ms 500 '<h1>Cannot read property &#39;remoteAddress&#39; of undefined</h1>\n<h2></h2>\n<pre></pre>\n' { 'X-Powered-By': 'Express', 'x-powered-by': 'Express' }

medisoft avatar Dec 06 '18 18:12 medisoft

I encountered the same problem. Could you please tell me if you solved it?

kingzevin avatar Mar 23 '20 16:03 kingzevin

Hello,

I thinks it's because (if you take a look at the index.js of the module, function createRes), the Response object "emulated" by app.runMiddleware doesn't have a res.connection property. So if somewhere in a middleware crossed by the path given, you use req.connection.remoteAdress, connection will be undefined and that will cause this error.

If i'm not mistaken, try to run the same path inside a request middleware with req.runMiddleware. In this case, the req and res normal object will be used, and you shouldn't have this error because req.connection exists.

To solve your problem, you can try to add connection property manually in the options argument app.runMiddleware('path/to/your/request', { connection: { remoteAddress: /fakeAdress/ } }, /callback/)

laekem34 avatar May 30 '20 09:05 laekem34

If you wish the create a fix, I will review and merge it.

Aminadav avatar Mar 13 '21 23:03 Aminadav