lwsjs / local-web-server

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

Wildcard hostnames #138

Closed antonkomarev closed 4 years ago

antonkomarev commented 4 years ago

I was wanted to run web server with wildcard subdomains:

ws --hostname *.test.localhost

This will be very useful to test multi-tenant applications, when each tenant served by the same frontend application, but served on the different subdomains:

75lb commented 4 years ago

I don't think Node.js supports wildcard subdomains. Can't you just use DNS?

You can create DNS records for tenant1.test.localhost and tenant2.test.localhost which resolve to 0.0.0.0, 127.0.0.1 or another valid IP address for your server.

https://stackoverflow.com/questions/34715797/how-to-use-the-optional-hostname-parameter-in-node-js-server-listen

antonkomarev commented 4 years ago

Subdomains are generated with random number, so there is no way to define all the possible cases. Usually tenant names contains random number at the end: tenant12321.test.localhost

75lb commented 4 years ago

If you run the ws command, the server will accept connections from any hostname which resolves to a valid IP address for your computer.

What do you need me to do? I'm not clear. You can already use any hostname (e.g. tenant12321.test.localhost) but the hostname must be correctly set up in DNS. I can't help you with your "random number" issue - a hostname will only work if it is configured in DNS.

antonkomarev commented 4 years ago

If I'm defining strict hostname it works as expected: ws --hostname test.localhost This command starts web server which listens requests from http://test.localhost

But I was wanted to write: ws --hostname *.test.localhost And from my point of view it should start webserver which listens requests from any subdomain: http://any.test.localhost, http://sub.test.localhost, http://domain.test.localhost, ...

I have configured localhost DNS which allows wildcards and using it by PHP webserver. But your server listens only strictly defined hostnames.

75lb commented 4 years ago

If you run this command:

ws --hostname localhost

the server will accept connections from both localhost and test.localhost.

antonkomarev commented 4 years ago

Sorry, one of my apps not working this way and I don't understand why is this happens. I've tried on another app right now and it's working without any issues. Need to try to find the reason.

75lb commented 4 years ago

The value you pass to --hostname must be a valid DNS entry. This script must return a valid IP.

const dns = require('dns').promises

async function start () {
  const addresses = await dns.lookup('test.localhost')
  console.log(addresses)
}

start().catch(console.error)