Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller gets mapped but returns 404 when trying to access routes #4269

Closed
DaCurse opened this issue Mar 11, 2020 · 6 comments
Closed

Controller gets mapped but returns 404 when trying to access routes #4269

DaCurse opened this issue Mar 11, 2020 · 6 comments
Labels
needs triage This issue has not been looked into

Comments

@DaCurse
Copy link

DaCurse commented Mar 11, 2020

Bug Report

Current behavior

When running my application, the console says that a certain controller's routes were mapped but when I try to acess them via postman I get a 404 error
image

My controller is connected to a module which has it's own application created using NestFactoryStatic, so this might be the issue. I have also tried creating various dummy routes, setting a global prefix, everything was logged to the console as it was working but it did not. Routes for the other application being created using another NestFactoryStatic worked without problem.

Input Code

Controller:

@Controller()
export class RedirectController {
	constructor(private readonly redirectService: RedirectService) {}

	@Get(':shortId')
	async redirect(
		@Param('shortId') shortId: string,
		@Res() res: Response,
	): Promise<void> {
		const originalUrl = await this.redirectService.getOriginalUrl(shortId);
		res.redirect(originalUrl);
	}
}

Module:

@Module({
	imports: [
		TypeOrmModule.forRoot(typeOrmRedirectConfig),
		TypeOrmModule.forFeature([LinkRepository], 'redirect'),
	],
	controllers: [RedirectController],
	providers: [RedirectService],
})
export class RedirectModule {}

main.ts:

async function bootstrap() {
	const server = new ExpressAdapter();

        // These routes work
	const apiFactory = new NestFactoryStatic();
	const apiApp = await apiFactory.create(ApiModule, server);
	apiApp.setGlobalPrefix('api/v1');
	await apiApp.init();

        // These get mapped but don't work
	const redirectFactory = new NestFactoryStatic();
	const redirectApp= await redirectFactory.create(RedirectModule, server);
	await redirectApp.init();

	http.createServer(server.getInstance()).listen(process.env.PORT || 3000);
}

Expected behavior

The routes should return a response

Environment


Nest version: 6.10.14
Updated all packages to latest version, the issue still occurs with nest 6.11.11
 
For Tooling issues:
- Node version: 12.6.1
- Platform:  Windows 10 64 Bit

Others:
Using yarn, vscodium
@DaCurse DaCurse added the needs triage This issue has not been looked into label Mar 11, 2020
@jmcdo29
Copy link
Member

jmcdo29 commented Mar 11, 2020

@DaCurse this is the same thing I warned you about on Discord yesterday relating to #4114. In short, the routes do get mapped, but they get mapped after a not found handler has already been mapped, so while they do exist in the route handler, the routes can never be called. #4186 is a PR with a possibility to fix it, if you'd like to take a look at the discussion there.

@DaCurse
Copy link
Author

DaCurse commented Mar 11, 2020

@jmcdo29 Thank you, I will follow that pull request

@DaCurse DaCurse closed this as completed Mar 11, 2020
@OysterD3
Copy link

I found a workaround await Promise.all([apiApp.init(), redirectApp.init()])

@j-a-h-i-r
Copy link

Still getting this error on 7.6.15

@jmcdo29
Copy link
Member

jmcdo29 commented Jul 2, 2021

@j-a-h-i-r please create a new issue with a minimum reproduction Repository.

@j-a-h-i-r
Copy link

@j-a-h-i-r please create a new issue with a minimum reproduction Repository.

Hi, I've created an issue #7384

@nestjs nestjs locked and limited conversation to collaborators Jul 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants