lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.21k stars 85 forks source link

Get serving list in my own middleware #74

Closed baklanovavpulter closed 6 years ago

baklanovavpulter commented 6 years ago

This is question.

I'm trying to write my own middleware to provide transparent proxy service.

module.exports = MiddlewareBase  => class TransparentProxy extends MiddlewareBase  {

    middleware (options) {
        return async (ctx, next) => {
            // Here i'm changing ctx object on some rules

            if (
                // ctx object refers to some local file
            ) {
                return await next();
            } else {
                return this.processRequestToRemoteServer(ctx);
            }
        }
    }

}
  1. How can I check that after some changes ctx object is still refers to some local file? I can get current serving port from options object that passed to middleware method, but I need also list of current binded host/ip. Can I have access to current serving list?
  2. Is there some built-in methods to provide remote requests? For now, I'm using lws-rewrite as example.
75lb commented 6 years ago
  1. I don't understand exactly what you are trying to achieve. Did you look at the ctx documentation?
  2. I recommend you make requests using node
baklanovavpulter commented 6 years ago
  1. I tryed to achieve this list:

$ ws Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000

  1. For now all working fine through req-then package (like in lws-rewrite). I just intrested is there some built-in methods or not.
75lb commented 6 years ago
  1. Ah, now I understand. You need this function.
$ node
> const util = require('lws/lib/util')
> util.getIPList()
[ { address: 'mbp.local' },
  { address: '127.0.0.1',
    netmask: '255.0.0.0',
    family: 'IPv4',
    mac: '00:00:00:00:00:00',
    internal: true,
    cidr: '127.0.0.1/8' },
  { address: '192.168.0.103',
    netmask: '255.255.255.0',
    family: 'IPv4',
    mac: 'd0:a6:37:e9:86:49',
    internal: false,
    cidr: '192.168.0.103/24' } ]
baklanovavpulter commented 6 years ago

What if some reasons, lws cannot bind to '127.0.0.1' but can on all other? I suppose, lws started with startup message:

$ ws Serving at http://mbp.local:8000, http://192.168.0.103:8000

but util.getIPList() returns me

[ { address: 'mbp.local' },
  { address: '127.0.0.1',
    netmask: '255.0.0.0',
    family: 'IPv4',
    mac: '00:00:00:00:00:00',
    internal: true,
    cidr: '127.0.0.1/8' },
  { address: '192.168.0.103',
    netmask: '255.255.255.0',
    family: 'IPv4',
    mac: 'd0:a6:37:e9:86:49',
    internal: false,
    cidr: '192.168.0.103/24' } ]

Or this cannot be happen?