0.1.0 • Published 4 years ago
@pyramation/url-domains v0.1.0
url domains
for use with express
why?
Because, express requires that you do things like this when you need req.subdomains
on localhost
:
if (env.SERVER_HOST === 'localhost') {
app.set('subdomain offset', 1);
}
and I believe the developer experience shouldn't include any off-by-one errors imo if you forgot this or your environments are changing and/or dynamic.
So why not just make a subdomains
object you can trust no matter what?
install
yarn add @pyramation/url-domains
middleware
import {
middleware as parseDomains
} from '@pyramation/url-domains';
app.use(parseDomains());
app.use(async (req, res, next) => {
// have fun!
await fn(req.urlDomains.domain, req.urlDomains.subdomains);
});
manual usage
import {
parseReq
} from '@pyramation/url-domains';
app.use(async (req, res, next) => {
const {
domain,
subdomains
} = parseReq(req);
// cheers!!
await fn(domain, subdomains);
});