express-serve-static-core#Send TypeScript Examples
The following examples show how to use
express-serve-static-core#Send.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: server.ts From eosio-contract-api with GNU Affero General Public License v3.0 | 5 votes |
constructor(readonly server: HTTPServer) {
this.express = express();
if (this.server.config.trust_proxy) {
this.express.set('trust proxy', 1);
}
this.express.set('etag', false);
this.express.set('x-powered-by', false);
this.express.use(((req, res, next) => {
res.setHeader('Last-Modified', (new Date()).toUTCString());
next();
}));
if (this.server.config.rate_limit) {
const client = this.server.connection.redis.nodeRedis;
const store = new RedisStore({
sendCommand: (...args: string[]): any => client.sendCommand(args),
prefix: 'eosio-contract-api:' + server.connection.chain.name + ':rate-limit:'
});
const keyGenerator = (req: express.Request): string => req.ip;
this.limiter = rateLimit({
windowMs: this.server.config.rate_limit.interval * 1000,
max: this.server.config.rate_limit.requests,
keyGenerator, store,
skip: (req: express.Request) => this.server.config.ip_whitelist.indexOf(req.ip) >= 0,
handler: (req: express.Request, res: express.Response): any => {
res.status(429).json({success: false, message: 'Rate limit'});
},
legacyHeaders: true,
standardHeaders: true
});
if (this.server.config.rate_limit.bill_execution_time) {
const limiter = this.limiter;
this.limiter = async (req: express.Request, res: express.Response, next: express.NextFunction): Promise<void> => {
const requestTime = Date.now();
const sendFn: Send = res.send.bind(res);
res.send = (data): express.Response => {
(async (): Promise<void> => {
const limitExceeded = Math.ceil((Date.now() - requestTime) / 1000) - 1;
const key = keyGenerator(req);
for (let i = 0; i < limitExceeded; i++) {
await store.increment(key);
}
})();
return sendFn(data);
};
limiter(req, res, next);
};
}
}
this.caching = expressRedisCache(
this.server.connection.redis.nodeRedis,
'eosio-contract-api:' + this.server.connection.chain.name + ':express-cache:',
this.server.config.cache_life || 0,
this.server.config.ip_whitelist || []
);
this.middleware();
this.routes();
}