subdomain option
I'm submitting a...
[ ] Regression
[ ] Bug report
[*] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Expected behavior
Is it possible currently to add routes per subdomain ? There is this library - https://www.npmjs.com/package/express-subdomain but i couldn't figure how to make it work
I believe we can pass a created express instance into the creation of nest application.
const express = express(); const nestApp = await NestFactory.create(mainModule, express, {});
After the nest application has been created. It used the passed express to create routes if I'm not mistaken.
With that, I guess we can retrieve the router from the passed express.
const express = express(); const nestApp = await NestFactory.create(mainModule, express, {}); nestApp.use(subdomain(api, express.Router));
There's no docs found regarding retrieving the routers created
yea but we do not have access to the route creation by nestjs controller, we can restrict acsess to a whole application. but what if i want 2 subdomains to go to diff routes?
Hi @hershko765 , I created the RouterModule which could help you versioning your api in easiest way.
I dose not support the subdomain options, but instead, you can versioning your Modules
see
... //imports
const routes: Routes = [
{
path: '/v1',
children: [CatsModuleV1, DogsModuleV1],
},
{
path: '/v2',
children: [CatsModuleV2, DogsModuleV2],
},
];
@Module({
imports: [
RouterModule.forRoutes(routes), // setup the routes
CatsModuleV1,
DogsModuleV1,
CatsModuleV2,
DogsModuleV2
], // as usual, nothing new
})
...
it maybe not you are looking for, but maybe it will help check out here more examples: Nest Router Module
its nice but thats not what i was looking for, i do not use NestJS for API - i use it to build a website (i feel so old), that has 2 seperate sections myapp.com and trade.myapp.com for example, it needs to be a subdomain, i might be able to hack it with middleware but i rather make it in the right way if possible
import * as express from 'express';
import * as http from 'http';
import subdomain = require('express-subdomain');
const instance = express();
const instance2 = express();
const apiRoutefun = (req,res,next)=>{
console.log('!!api');
instance2(req,res);
};
const apiRoute = subdomain('api', apiRoutefun);
instance.use(apiRoute);
const server = http.createServer((res,req)=>{
instance(res,req);
});
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule, instance2 as any);
// await app.listen(3000);
app.init();
server.listen(4000);
}
bootstrap();
just a example...
Are you not starting two servers? One from nest and one from the express instance?
i do not want to start 2 servers, i want to add subdomain route option, same as controller is routing by uri i want it also to route by host or subdomain, its an option that available in any web framework today in any language. i can however configured it in nginx to direct "sub.myapp.com" to "myapp.com/sub", and thats what i will do for now, thanks for trying to help
@whtiehack not sure what you did there, did you just created 2 express instances one works with nestjs and one not?
Hmm, I think you can try to dig in the source code of them and try adding the subdomain module inside it. If it works, maybe you could file a PR. That would be great help for the community :)
yea maybe i will ! :), but after this project, urgent stuff
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.