# Customize 404
The guide shows you how you can customize the 404 response error emitted by Express.js.
To begin, create a new Middleware named NotFoundMiddleware
:
import {Middleware, Res, Next} from "@tsed/common";
@Middleware()
export class NotFoundMiddleware {
use(@Res() response: Res, @Next() next: Next) {
// Json response
response.status(404).json({status: 404, message: 'Not found'});
// Or with ejs
response.status(404).render("404.ejs", {}, next);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Then register your middleware on the $afterRoutesInit
in your Server:
import {Inject} from "@tsed/di";
import {Configuration, PlatformApplication} from "@tsed/common";
import {NotFoundMiddleware} from "./middlewares/NotFoundMiddleware";
@Configuration({
// ...
})
export class Server {
@Inject()
app: PlatformApplication;
public $afterRoutesInit() {
this.app.use(NotFoundMiddleware);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- Session & cookies
- Passport.js
- TypeORM
- Mongoose
- GraphQL
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Throw HTTP Exceptions
- Customize 404
- AWS
- Jest
- Seq
- Controllers
- Providers
- Model
- Converters
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Injection scopes
- Custom providers
- Custom endpoint decorator
- Testing