expressjs / vhost

virtual domain hosting

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

vhost for other web frameworks.

opened this issue · comments

Hello, there. I'm sorry if it has already been asked, but I don't find it. My primary web server is based on express, but I want to manage my API with Hapi.js and serve it at api.example.com. So my question is: "can I use vhost for managing web frameworks other than connect, express, etc. by architecture?" Thanks :)

I have sort of used Hapi.js before, but not enough to know. This module is under the "expressjs" org in GirHub, so it is really only designed for Node.js core http module, connect, express, and similar frameworks. Hapi.js is very different and I don't think this module would be able to support it, but if you can figure it out and send a PR here, that would work :)!

Ultimately the "thing" that you pass to this module is agnostic to the underlying framework, as it is simply a function with the signature "function(req,res){}" where req and res are from Node.js core HTTP. Perhaps Hapi.js provides a way to call the app given that?

Basically, look around in Hapi.js docs for how to do "http.createServer(some_hapi_var)" and then all you need to do us provide "some_hapi_var" to this module as your sub host and it'll work just fine :)

I'll try to look as well, but cannot promise, but I also need to at least update the docs to show more generic sub server uses for stuff like this.

I tried to look at the Hapi.js API docs on my phone, but there is just too much; I'll resume looking whenever I get to a computer.

Looks like this should work:

var express = require('express')
var Hapi = require('hapi')
var vhost = require('vhost')

var app = express()
var server = new Hapi.Server()

// setup your Hapi server
// including creating a single connection and starting the server

app.use(vhost('api.example.com', function (req, res) {
  server.listener.emit('request', req, res)
}))

That seems to work for me. I cannot figure out a way to do this in Hapi without creating at least one connection and then starting the server, which means you'll end up having your sub host listening on something in addition to being called using this module.

@dougwilson Thanks, you really rescue me.