Giter Site home page Giter Site logo

Comments (15)

dougwilson avatar dougwilson commented on May 8, 2024

This module does not use req.host at all; it is not dependent on Express. We directly read the Host header: https://github.com/expressjs/vhost/blob/master/index.js#L77

Any issues with req.host cannot possibly be affecting this module. Can you provide full code and versions and how I can reproduce the issue?

from vhost.

gaurav21r avatar gaurav21r commented on May 8, 2024

@dougwilson Thank you for such a quick response 💯 👍 ! I'll try to push some code to a dummy repo so you can run it!

from vhost.

dougwilson avatar dougwilson commented on May 8, 2024

Oh, sorry, this is not a bug in anyway. You can see everywhere in the README, it says we are only routing on hostname, not host. This module does not in any way support port-based virtual hosts. This would have to be a feature you could make a PR to implement, though.

from vhost.

gaurav21r avatar gaurav21r commented on May 8, 2024

Oh Sorry it must've escaped me! We are stripping the port here https://github.com/expressjs/vhost/blob/master/index.js#L88 I really think this would be needed for local environments as we do not usually work with multiple domains but ports.

I'd love to submit a PR! But how should I go about doing it? According to me I can just modify the above lines but that would mean some unexpected changes for users.

from vhost.

dougwilson avatar dougwilson commented on May 8, 2024

I will need some time to refresh myself on how this module works before I can provide some direction. I should be able to get back to you on this within a week :)

from vhost.

dougwilson avatar dougwilson commented on May 8, 2024

In general, I would probably expect the syntax similar to that of Apache in some way: https://httpd.apache.org/docs/current/vhosts/examples.html

This would mean probably an optional :<port> at the end of the string, and of course you would need to account for the fact that IPv6 addresses have colons in them in your implementation.

from vhost.

gaurav21r avatar gaurav21r commented on May 8, 2024

Thanks @dougwilson Just to help you, the API is simple, as mentioned in README, its just vhost(hostname, handle)

Now users who were sending requests to http://doug.wilson.com:3000 are used to writing app.use(vhost(doug.wilson.com), route)
and I propose they should now write app.use(vhost(doug.wilson.com:3000)).

I can figure out the internals of vhost to send a PR. Could you just approve the right API from outside?

Btw I just did a Ctrl F on the README and couldn't find an explicit entry on ports. Maybe we could add it? :)

from vhost.

gaurav21r avatar gaurav21r commented on May 8, 2024

Following the example from Apache, is something like this fine?

A => window.fetch('http://doug.wilson.com:8000')
B => window.fetch('http://doug.wilson.com:3000')
C => window.fetch('http://doug.wilson.com')

app.use(vhost('doug.wilson.com:8000', route)) // Matches only A
app.use(vhost('doug.wilson.com:3000', route)) // Matches only B
app.use(vhost('doug.wilson.com', route)) // Matches A, B and C
app.use(vhost('doug.wilson.com:80', route)) // Matches only C

So the user has to explicitly declare a port within vhost to match it. Otherwise, its not breaking behaviour for current users just writing the domain without port. We can anyway bump up a version number to be sure.

I think Apache is also working this way!

Thanks for the heads up on the IPv6! I would have most certainly missed that 😛

from vhost.

dougwilson avatar dougwilson commented on May 8, 2024

The :80 example is not very good, because if you do that, then you'd also have to know if it was SSL or not, to determine if it should match against 80 or 443. Also, I just realized something: the Node.js server only lets you listen against a single port per server. If you are listening on multiple different ports, you had to have done that manually. Why would you not simply put up three different apps on the three different ports?

from vhost.

gaurav21r avatar gaurav21r commented on May 8, 2024

It's kind of complicated. You see we are emulating different environments in different ports. Since we have only one hostname localhost in the local dev environment. Now we are using an environment variable (which I believe is standard practice along with a config file) on the client side request part to control which environment we are requesting in.

The same environment variable is there on the server side. But when its on vhost() it gets ignored and no routes get hit.

Eg:

Client:
A => window.fetch('http://' + process.env.HOST)

Server:
app.use(vhost(process.env.HOST, route))

when process.env.HOST = 'localhost' everything is fine, but when its localhost:3000, vhost routes never get hit :(

from vhost.

dougwilson avatar dougwilson commented on May 8, 2024

Sure, but I would argue that, at least for that specific problem, it comes down to misunderstanding the documentation, and what a "hsotname" is vs a "host" (and defining that is really outside of the scope of this document, as they are general terms). If that was the summation of your problem, I would say that you should only provide the hostname as the first argument of the vhost factory, not the host :)

from vhost.

gaurav21r avatar gaurav21r commented on May 8, 2024

@dougwilson Our app (rather a platform for apps) gets a bit more complicated. In production we use proxying and I know that here we actually do have a problem bigger than documentation! What if these requests are coming from an nginx / node proxy and therefore multiple requests from diffrent ports all hitting the node server ona s ingle port but still have to be differentiated with vhost?

I know that nginx forwarding / load balancing is a fairly common use case in node?

from vhost.

dougwilson avatar dougwilson commented on May 8, 2024

Hi @gaurav21r, that is a good point, but I think that hasn't come up in the 5+ years of existence because having production differentiation on ports is very rare, compared to by hostnames, and even then, a feel like typically the edge node does the sharding to the backend, which doesn't seem to be the case in your example.

from vhost.

horaciogiovine avatar horaciogiovine commented on May 8, 2024

Hi @dougwilson, do you have any update on this? id really appreciate it!
Im too trying to use it with a port number, as i have to develop in localhost, and later ill use it with domains when in production.

from vhost.

dougwilson avatar dougwilson commented on May 8, 2024

@horaciogiovine was there something I was supposed to be doing in order to prove an update on something? From my remembering and re-reading this issue, @gaurav21r was maybe going to prove a PR if up to it. There is actually nothing left on this issue, so if you want to provide PR, @horaciogiovine please do!

from vhost.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.