http#OutgoingMessage TypeScript Examples
The following examples show how to use
http#OutgoingMessage.
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: serve-static-middleware.ts From malagu with MIT License | 5 votes |
async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
this.config.options.baseHref = this.path;
const method = ctx.request.method;
if (!(method === HttpMethod.GET || method === HttpMethod.HEAD) || ctx.request.query['static'] === 'skip') {
await next();
return;
}
if (this.config.apiPath && await this.requestMatcher.match(this.config.apiPath)) {
await next();
return;
}
if (this.config.path && !await this.requestMatcher.match(this.config.path)) {
await next();
return;
}
const executor = (resolve: any, reject: any) => {
const opts = this.config.options;
if (!opts.setHeaders) {
opts.setHeaders = (res: OutgoingMessage, path: string) => {
if (opts.headers) {
Object.keys(opts.headers).forEach(key => res.setHeader(key, opts.headers[key]));
}
if ((mime as any).lookup!(path) === MediaType.TEXT_HTML) {
// Custom Cache-Control for HTML files
res.setHeader(HttpHeaders.CACHE_CONTROL, `public, max-age=${opts.htmlMaxAge / 1000}`);
}
};
}
serveStatic(this.config.root, this.config.options)(ctx.request as any, ctx.response as any, ((err: any) => {
const url = ctx.request.url;
if (url !== '/index.html') {
if (!this.config.spa) {
return;
}
ctx.request.url = '/index.html';
executor(resolve, reject);
} else if (err) {
reject(err);
} else {
next().then(resolve).catch(reject);
}
}) as any);
};
return new Promise(executor);
}