I need Routing based on pattern against Host name and subdomain,(ex: cms.localhost) OR (cms.example.com)
@injectable()
@Controller('/')
export class HomeController {
@Get('/')
public get(req:express.Request,res:express.Response) {
let unitOfWork = new UnitOfWork();
unitOfWork.CustomersRepo.GetAll().then(result=>{
res.render('home', {customerList : result});
}).catch(function(err) {
res.render('error', {error : {err}});
});
}
}
in above code sample , how can I use something like:
@Get('cms.example/')
To route into this class if user try to browse to subdomain.
There is a module like :
let vhost = require('vhost');
To do search pattern on hostname but how can I use something like vhost on class by decorators.
The decorators @Controller('/') and @Get('/') use the domain as base path.
| Controller | Method | Decorators | Path |
|---|---|---|---|
| / | GET | @Controller("/") and Get("/") | http://cms.example.com/ |
| / | GET | @Controller("/") and Get("/") | http://cms.localhost/ |
| /users | GET | @Controller("/users") and Get("/") | http://cms.example.com/users |
| /users | GET | @Controller("/users") and Get("/") | http://cms.localhost/users |
As you can see the domain name should not affect the controllers and method decorators.
May @CoderAjay or @lholznagel can help?
As I mention above there is a module which is called "vhost" make able to do this approach but I need to do this routing by decorators or if there is no way ,using a way to inject this middleware ( ex: vhost) into each controller. ( I prefer the first one ,doing by decorators)
Hm good question. I always do it like @remojansen listed it. I route subdomains and such with nginx to the server it belongs.
Did you tried your example from above? Just taking a quick look at the vhost module, maybe you can try to do something like this: @Get(vhost('cms.local')). Really don´t know if this works and if inversify-express will detect this.
Thanks for the help @lholznagel 👍
@hkarimpoor what do you want to achieve? Invoke a different controller or method based on the domain? Would this https://www.npmjs.com/package/express-subdomain work for you? If that middleware works we can add a change so you will be able to use it.
@lholznagel thanks but @Get(vhost('cms.local')) doesnot work. @remojansen my final goal is writing different controller which routes based on domain and subdomain for example : if user type cms.mydomain.com route to admin home page if user type cms.mydomain.com/customer/add route to admin adding customer page . . . if user type mydomain.com route to normal website home page .
how can I achieve this approach by writing something like this :
@injectable()
@Controller('cms/')
export class HomeController {
@Get('/customer/add')
public get(req:express.Request,res:express.Response) {
}
}
Any Suggestion ? I am really need this feature if you can help
Well with the url given from above this would work:
@injectable()
@Controller('/')
export class HomeController {
@Get('/')
public get(req: Request, res: Response) {
// admin page
return '/';
}
}
@injectable()
@Controller('/customer')
export class CustomerController {
// normaly post here get for testing
@Get('/add')
public get(req: Request, res: Response) {
// add customer
return '/customer/add';
}
}
Then I tried to make subdomains working. My only solution was this:
app.use(vhost('cms.example.localhost', (request: Request, response: Response) => {
response.redirect(request.originalUrl);
}));
The problem with this is that you can access all routes, without subdomain or with a changed subdomain.
cms.example.localhost -> works
asd.example.localhost -> works
@hkarimpoor can you confirm that https://www.npmjs.com/package/express-subdomain could be an option for you? if it would work I can try to implement a fix so you can use it...
@remojansen thanks for reply, I confirm express-subdomain could be really helpful. OR express-vhost or any other middleware which can search against domain part.
My Expectation: cms.example.localhost -> redirect to CMS home pages cms.example.localhost/products -> redirect to CMS productspages that Admin can edit products
www.example.localhost -> redirect to normal website home page www.example.localhost/products -> redirect to normal website products page that only displays products list
abcd.example.localhost -> redirect to not found page
@remojansen A very necessary thing. For example, I used vhost in my projects. It would be very cool if you implement this feature