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.


export declare class RestServer extends BaseMiddlewareRegistry implements Server, HttpServerLike 

Extends: BaseMiddlewareRegistry

Implements: Server, HttpServerLike


const app = new MyApplication();

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('');


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


Property Modifiers Type Description
_expressApp? protected express.Application (Optional)
_httpHandler protected HttpHandler  
_httpServer protected HttpServer | undefined  
_requestHandler protected HttpRequestListener  
config readonly RestServerResolvedConfig  
httpHandler <p>protected</p><p>readonly</p> HttpHandler  
httpServer readonly HttpServer | undefined  
listening readonly boolean  
OASEnhancer readonly OASEnhancerService  
oasEnhancerService protected OASEnhancerService Handle incoming HTTP(S) request by invoking the corresponding Controller method via the configured Sequence.
requestHandler readonly HttpRequestListener  
rootUrl readonly 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 readonly 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’.


Method Modifiers Description
_applyExpressSettings() protected Apply express settings.
_handleHttpRequest(request, response) protected  
_setupHandlerIfNeeded() protected  
_setupOASEnhancerIfNeeded() protected  
_setupOpenApiSpecEndpoints() protected Mount /openapi.json, /openapi.yaml for specs and /swagger-ui, /explorer to redirect to externally hosted API explorer
_setupRequestHandlerIfNeeded() protected  
_unexpectedErrorHandler() protected Get an Express handler for unexpected errors
addOpenApiSpecEndpoint(path, form, router)   Add a new non-controller endpoint hosting a form of the OpenAPI spec.
api(spec)   <p>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.</p><p>Note that this will override any routes defined via decorators at the controller level (this function takes precedent).</p>
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.
exportOpenApiSpec(outFile, log)   Export the OpenAPI spec to the given json or yaml file
getApiSpec(requestContext)   <p>Get the OpenAPI specification describing the REST API provided by this application.</p><p>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')</p><p>If the optional requestContext is provided, then the servers list in the returned spec will be updated to work in that context. Specifically: 1. if config.openApi.setServersFromRequest is enabled, the servers list will be replaced with the context base url 2. Any servers entries with a path of / will have that path replaced with requestContext.basePath</p>
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(sequenceClass)   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
stop()   Stop this REST API’s HTTP/HTTPS server.