@travetto/rest-express
v5.0.17
Published
Express provider for the travetto rest module.
Downloads
612
Maintainers
Readme
Express REST Source
Express provider for the travetto rest module.
Install: @travetto/rest-express
npm install @travetto/rest-express
# or
yarn add @travetto/rest-express
The module is an express provider for the RESTful API module. This module provides an implementation of RestApplication for automatic injection in the default Rest server.
Customizing Rest App
Code: Customizing the Express App
import { TimeUtil } from '@travetto/runtime';
import { Injectable } from '@travetto/di';
import { ExpressRestServer } from '@travetto/rest-express';
declare let rateLimit: (config: { windowMs: number, max: number }) => ((req: Express.Request, res: Express.Response) => void);
@Injectable({ primary: true })
class CustomRestServer extends ExpressRestServer {
override async init() {
const app = await super.init();
const limiter = rateLimit({
windowMs: TimeUtil.asMillis(15, 'm'), // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
// apply to all requests
app.use(limiter);
return app;
}
}
Default Middleware
When working with an express applications, the module provides what is assumed to be a sufficient set of basic filters. Specifically:
Code: Configured Middleware
const app = express();
app.set('query parser', 'simple');
app.disable('x-powered-by');
app.use(compression());