Here are some tips for application setup to help avoid common pitfalls and mistakes.
RestApplication when using
If you want to use
RestServer from the
@loopback/rest package, we recommend
RestApplication in your app instead of manually binding
RestApplication already uses
RestComponent and makes
useful functions in
handler() available at the app level.
This means you can call the
RestServer functions to perform all of your
server-level setups in the app constructor without having to explicitly retrieve
an instance of your server.
Serve static files
RestServer allows static files to be served. It can be set up by calling
server.static(['/html', '/public'], rootDirForHtml);
Static assets are not allowed to be mounted on
/ to avoid performance penalty
/ matches all paths and incurs file system access for each HTTP request.
The static() API delegates to serve-static to serve static files. Please see https://expressjs.com/en/starter/static-files.html and https://expressjs.com/en/4x/api.html#express.static for details.
Warning: The static assets are served before LoopBack sequence of actions. If an error is thrown, the
reject action will NOT be triggered.
Use unique bindings
Use binding names that are prefixed with a unique string that does not overlap
with LoopBack’s bindings. As an example, if your application is built for your
employer FooCorp, you can prefix your bindings with
// This is unlikely to conflict with keys used by other component developers // or within loopback itself! app.bind('fooCorp.widgetServer.config').to(widgetServerConfig);
Avoid use of
We provide the
function for scenarios where you cannot asynchronously retrieve your bindings,
such as in constructor bodies.
However, the number of scenarios in which you must do this are limited, and you
should avoid potential race conditions and retrieve your bindings asynchronously
function whenever possible.
Use caution with singleton binding scopes
By default, bindings for controllers will instantiate a new instance whenever they are injected or retrieved from their binding. Your application should only set singleton binding scopes on controllers when it makes sense to do so.