A sort a virtual host support in MacroExpress
nathanfallet opened this issue · 1 comments
There is an HTTP header called Host
, so it could be great to have a native way to filter host to have different routes depending of them.
For example, you can have a example.com
domain and filter host to have different routes depending on if we call example.com
or api.example.com
(and many more possibilities)
Yes, virtual hosts. You should be able to accomplish that using mounting. Just create an app object for each host, and then mount it into the main one, explained over here:
https://github.com/Macro-swift/MacroExpress/blob/master/Sources/express/Express.swift#L58
The middleware should be really trivial, something like that (untested):
function vhost(_ host: String, _ middleware: @escaping Middleware) -> Middleware {
return { req, res, next in
guard (req.getHeader("Host") as? String).lowercased() == host.lowercased() else {
return next()
}
middleware(req, res, next)
}
}
and then you can do
app.use(vhost("api.xyz.de", APIApp.middleware))
(TBH I think I didn't actually try mounting w/ MacroExpress yet, though it should work).
Before we add such a middleware, I'd like to explore how Node Express is usually doing this (to avoid introducing a different API).