Page Contents

Home > @loopback/rest > RestServer

RestServer class

A REST API server for use with Loopback. Add this server to your application by importing the RestComponent.

Signature:

export declare class RestServer extends Context implements Server, HttpServerLike 

Constructors

Constructor Modifiers Description
(constructor)(app, config)   Creates an instance of RestServer.

Properties

Property Modifiers Type Description
_expressApp   express.Application  
_httpHandler   HttpHandler  
_httpServer   HttpServer | undefined  
_requestHandler   HttpRequestListener Handle incoming HTTP(S) request by invoking the corresponding Controller method via the configured Sequence.
config   RestServerResolvedConfig  
httpHandler   HttpHandler  
listening   boolean  
requestHandler   HttpRequestListener  
rootUrl   string | undefined The root url for the server without the basePath. For example, the value will be ‘http://localhost:3000’ regardless of the basePath.
url   string | undefined The base url for the server, including the basePath if set. For example, the value will be ‘http://localhost:3000/api’ if basePath is set to ‘/api’.

Methods

Method Modifiers Description
_applyExpressSettings()   Apply express settings.
_handleHttpRequest(request, response)    
_onUnhandledError(req, res, err)    
_setupHandlerIfNeeded()    
_setupOpenApiSpecEndpoints()   Mount /openapi.json, /openapi.yaml for specs and /swagger-ui, /explorer to redirect to externally hosted API explorer
_setupRequestHandlerIfNeeded()    
api(spec)   Set the OpenAPI specification that defines the REST API schema for this server. All routes, parameter definitions and return types will be defined in this way.Note that this will override any routes defined via decorators at the controller level (this function takes precedent).
basePath(path)   Configure the basePath for the rest server
bodyParser(bodyParserClass, address)   Bind a body parser to the server context
controller(controllerCtor)   Register a controller class with this server.
getApiSpec()   Get the OpenAPI specification describing the REST API provided by this application.This method merges operations (HTTP endpoints) from the following sources: - app.api(spec) - app.controller(MyController) - app.route(route) - app.route('get', '/greet', operationSpec, MyController, 'greet')
handler(handlerFn)   Configure a custom sequence function for handling incoming requests.
mountExpressRouter(basePath, router, spec)   Mount an Express router to expose additional REST endpoints handled via legacy Express-based stack.
redirect(fromPath, toPathOrUrl, statusCode)   Register a route redirecting callers to a different URL.
route(verb, path, spec, controllerCtor, controllerFactory, methodName)   Register a new Controller-based route.
route(verb, path, spec, handler)   Register a new route invoking a handler function.
route(route)   Register a new generic route.
sequence(value)   Configure a custom sequence class for handling incoming requests.
start()   Start this REST API’s HTTP/HTTPS server.
static(path, rootDir, options)   Mount static assets to the REST server. See https://expressjs.com/en/4x/api.html#express.static
stop()   Stop this REST API’s HTTP/HTTPS server.

Example

const app = new MyApplication();
app.component(RestComponent);

To add additional instances of RestServer to your application, use the .server function:

app.server(RestServer, 'nameOfYourServer');

By default, one instance of RestServer will be created when the RestComponent is bootstrapped. This instance can be retrieved with app.getServer(RestServer), or by calling app.get('servers.RestServer') Note that retrieving other instances of RestServer must be done using the server’s name:

const server = await app.getServer('foo')
// OR
const server = await app.get('servers.foo');