question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

req.vhost is undefined

See original GitHub issue

This is my main code, but I get req.vhost is undefined, any idea what can cause it?

app.use(function(req, res, next){
console.log(req.vhost)
  var username = req.vhost[0] // username is the "*"
  console.log(username)
 
  // pretend request was for /{username}/* for file serving
  req.originalUrl = req.url;
  console.log('req '+ req.originalUrl)
  req.url = '/' + username + req.url
  console.log(req.url)


  next()
})

app.use(vhost('*.myapp.local', indexRouter))

if I delete the console logs in the middleware the right route is rendered, but it is important to have access to the req.vhost[0] to render dynamic data based on the subdomain the user is typing

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dougwilsoncommented, Nov 29, 2018

Hi @mrrovot , the req.vhost property is not populated until the request actually passes though this middleware. In your example, you are accessing req.vhost before this middleware is ever invoked. In addition, the req.vhost property is only available within the defined handler, which in your case is the indexRouter.

I hope that helps.

0reactions
mrrovotcommented, Nov 29, 2018

the req.vhost[0] is commented out, I meant where there is console.log(req.vhost) I couldn’t access the[0]but as I was recreating a simple app with the error to send you, it now it works!, probably it was something I mistyped last night

thanks for the help

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - VHost in ExpressJS accessing pages of main website
Suppose you have a contact page in the main domain, you can make changes like. sub.get('/contact', function(req, res){ return res.
Read more >
@tryghost/vhost-middleware - npm
When host is matched and the request is sent down to a vhost handler, the req.vhost property will be populated with an object....
Read more >
"Virtual Host/WebGroup not Found" Error When Submitting ...
This problem is usually related to an incorrect configuration of a virtual host, or a necessary component that has not been started. Resolving ......
Read more >
Apache Default/Catch-All Virtual Host? - Server Fault
A default vhost never serves a request that was sent to an address/port that is used for name-based vhosts. If the request contained...
Read more >
An In-Depth Discussion of Virtual Host Matching ... - setgetweb.com
In other words the main_server only catches a request for an unspecified address/port combination (unless there is a _default_ vhost which matches that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found