To serve static files, this.static()
can be used:
import path from 'path';
export class TodoListApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
super(options);
// ...
this.static('/', path.join(__dirname, '../../public'));
}
}
While LoopBack 4 does use Express.js internally to re-use some of its features, it does not expose the entire API under normal circumstances.
There are some bindings under RestBindings.Http
that can be used to inject the Express.js RequestContext, Request, or Response objects into a controller. However, this should be considered a last resort.
If accessing these bindings are necessary, it is strongly recommended to inject them into a controller to take advantage of the lifecycle management and booters in LoopBack 4:
// /src/controllers/redirect.controller.ts
import {inject} from '@loopback/context';
import {get, Response, RestBindings} from '@loopback/rest';
export class RedirectController {
constructor(@inject(RestBindings.Http.RESPONSE) private res: Response) {}
// Map to `GET /redirect`
@get('/redirect', {
responses: {
'302': {
description: 'Redirect Response',
},
},
})
redirect(): void {
this.res.setHeader('x-secret-sauce', 'Sugar, spice and everything nice.');
this.res.redirect('https://www.example.com/');
}
}
The above code snippet shows how to set a response header and return a redirect. Though any parts of the Express.js Response API can be used.
Further reading: