Skip to content

Commit

Permalink
fix: optimize serving static files
Browse files Browse the repository at this point in the history
Optimize serving static files
  • Loading branch information
Hage Yaapa committed Oct 15, 2018
1 parent a2ab86d commit 6feb7d9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
21 changes: 11 additions & 10 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
Send,
} from './types';
import {ServerOptions} from 'https';
import * as HttpErrors from 'http-errors';

const debug = require('debug')('loopback:rest:server');

Expand Down Expand Up @@ -190,6 +191,15 @@ export class RestServer extends Context implements Server, HttpServerLike {
this._setupRequestHandler();

this.bind(RestBindings.HANDLER).toDynamicValue(() => this.httpHandler);

// LB4's static assets serving router
const staticAssetsRouter = new Route('get', '*', {responses: {}}, () => {
console.log('>> staticAssetsRouter HANDLER');
// @bajtos How do we access req and res here?
return 'HELLO!';
});

this.httpHandler.registerRoute(staticAssetsRouter);
}

protected _setupRequestHandler() {
Expand Down Expand Up @@ -243,7 +253,6 @@ export class RestServer extends Context implements Server, HttpServerLike {
protected _setupRouterForStaticAssets() {
if (!this._routerForStaticAssets) {
this._routerForStaticAssets = express.Router();
this._expressApp.use(this._routerForStaticAssets);
}
}

Expand Down Expand Up @@ -596,15 +605,7 @@ export class RestServer extends Context implements Server, HttpServerLike {
* @param rootDir The root directory from which to serve static assets
* @param options Options for serve-static
*/
static(path: PathParams, rootDir: string, options?: ServeStaticOptions) {
const re = pathToRegExp(path, [], {end: false});
if (re.test('/')) {
throw new Error(
'Static assets cannot be mount to "/" to avoid performance penalty.',
);
}
this._routerForStaticAssets.use(path, express.static(rootDir, options));
}
static(path: PathParams, rootDir: string, options?: ServeStaticOptions) {}

/**
* Set the OpenAPI specification that defines the REST API schema for this
Expand Down
5 changes: 5 additions & 0 deletions packages/rest/src/router/router-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export abstract class BaseRouter implements RestRouter {
else return this.findRouteWithPathVars(request);
}

getStaticAssetsRouter() {
let route = this.routesWithoutPathVars['/get/*'];
return createResolvedRoute(route, {});
}

list() {
let routes = Object.values(this.routesWithoutPathVars);
routes = routes.concat(this.listRoutesWithPathVars());
Expand Down
12 changes: 7 additions & 5 deletions packages/rest/src/router/routing-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export interface RestRouter {
*/
find(request: Request): ResolvedRoute | undefined;

getStaticAssetsRouter(): ResolvedRoute;

/**
* List all routes
*/
Expand Down Expand Up @@ -136,6 +138,8 @@ export class RoutingTable {
* @param route A route entry
*/
registerRoute(route: RouteEntry) {
// console.log(`>> ROUTING TABLE registerRoute A`);
// console.log(route);
// TODO(bajtos) handle the case where opSpec.parameters contains $ref
// See https://github.com/strongloop/loopback-next/issues/435
if (debug.enabled) {
Expand Down Expand Up @@ -180,10 +184,8 @@ export class RoutingTable {
return found;
}

debug('No route found for %s %s', request.method, request.path);
throw new HttpErrors.NotFound(
`Endpoint "${request.method} ${request.path}" not found.`,
);
const staticAssetsRouter = this._router.getStaticAssetsRouter();
return staticAssetsRouter;
}
}

Expand Down Expand Up @@ -290,7 +292,7 @@ export class Route extends BaseRoute {
verb: string,
path: string,
public readonly spec: OperationObject,
protected readonly _handler: Function,
protected readonly _handler: Function, // <-- doesn't this Function have a signature?
) {
super(verb, path, spec);
}
Expand Down

0 comments on commit 6feb7d9

Please sign in to comment.