In my Angular app, if I load the home page /
and then navigate to, say, /products
, it works fine (it's a lazy-loaded module). But if now I reload the page, the browser makes a GET /products
call to the server, which results in a 404.
The solution is to send index.html
and the Angular app is back on rails. So in Express I do app.all("*", (req,res) => { res.sendFile("index.html") })
and it works.
How to do the same thing in Nest?
There is a @All
decorator, but each controller in a given component handles a subroute, for instance @Controller("cats")
will match /cats
routes, so if I add @All
in this controller, it will match only /cats/*
, not *
.
Must I really create a whole separate module with a controller, just for this? That's what I did
@Controller() // Matches "/"
export class GenericController {
@All() // Matches "*" on all methods GET, POST...
genericFunction(){
console.log("Generic route reached")
}
}
And in my main module :
@Module({
imports: [
ItemsModule, // Other routes like /items
GenericModule, // Generic "*" route last
],
})
It works, but it seems overkill. Is this the way to go or is there a simpler trick?
So, will be best to use global-scoped
exception filter.
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
app.useGlobalFilters(new NotFoundExceptionFilter());
await app.listen(3000);
}
bootstrap();
NotFoundExceptionFilter:
import { ExceptionFilter, Catch, NotFoundException } from '@nestjs/common';
import { HttpException } from '@nestjs/common';
@Catch(NotFoundException)
export class NotFoundExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
// here return `index.html`
}
}
Maybe it will not work, will test later
You don't need to create a separated GenericModule
. However, GenericController
is fully valid and you approach is definitely a good one. The question is rather what would you like to achieve using this generic route. If handling "Route not found" error is your requirement, a better choice is an exception filter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With