UNPKG

@loopback/docs

Version:

Documentation files rendered at [https://loopback.io](https://loopback.io)

101 lines (80 loc) 7.69 kB
--- lang: en title: 'API docs: rest.restserver' keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI sidebar: lb4_sidebar editurl: https://github.com/loopbackio/loopback-next/tree/master/packages/rest permalink: /doc/en/lb4/apidocs.rest.restserver.html --- <!-- Do not edit this file. It is automatically generated by API Documenter. --> [Home](./index.md) &gt; [@loopback/rest](./rest.md) &gt; [RestServer](./rest.restserver.md) ## RestServer class A REST API server for use with Loopback. Add this server to your application by importing the RestComponent. <b>Signature:</b> ```typescript export declare class RestServer extends BaseMiddlewareRegistry implements Server, HttpServerLike ``` <b>Extends:</b> [BaseMiddlewareRegistry](./express.basemiddlewareregistry.md) <b>Implements:</b> [Server](./core.server.md)<!-- -->, [HttpServerLike](./rest.httpserverlike.md) ## Example ```ts const app = new MyApplication(); app.component(RestComponent); ``` To add additional instances of RestServer to your application, use the `.server` function: ```ts 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: ```ts const server = await app.getServer('foo') // OR const server = await app.get('servers.foo'); ``` ## Constructors | Constructor | Modifiers | Description | | --- | --- | --- | | [(constructor)(app, config)](./rest.restserver._constructor_.md) | | Creates an instance of RestServer. | ## Properties | Property | Modifiers | Type | Description | | --- | --- | --- | --- | | [\_expressApp?](./rest.restserver._expressapp.md) | | express.Application | <i>(Optional)</i> | | [\_httpHandler](./rest.restserver._httphandler.md) | | [HttpHandler](./rest.httphandler.md) | | | [\_httpServer](./rest.restserver._httpserver.md) | | [HttpServer](./http-server.httpserver.md) \| undefined | | | [\_requestHandler](./rest.restserver._requesthandler.md) | | [HttpRequestListener](./rest.httprequestlistener.md) | | | [config](./rest.restserver.config.md) | | [RestServerResolvedConfig](./rest.restserverresolvedconfig.md) | | | [httpHandler](./rest.restserver.httphandler.md) | | [HttpHandler](./rest.httphandler.md) | | | [httpServer](./rest.restserver.httpserver.md) | | [HttpServer](./http-server.httpserver.md) \| undefined | | | [listening](./rest.restserver.listening.md) | | boolean | | | [OASEnhancer](./rest.restserver.oasenhancer.md) | | [OASEnhancerService](./openapi-v3.oasenhancerservice.md) | | | [oasEnhancerService](./rest.restserver.oasenhancerservice.md) | | [OASEnhancerService](./openapi-v3.oasenhancerservice.md) | Handle incoming HTTP(S) request by invoking the corresponding Controller method via the configured Sequence. | | [requestHandler](./rest.restserver.requesthandler.md) | | [HttpRequestListener](./rest.httprequestlistener.md) | | | [rootUrl](./rest.restserver.rooturl.md) | | string \| undefined | The root url for the server without the basePath. For example, the value will be 'http://localhost:3000' regardless of the <code>basePath</code>. | | [url](./rest.restserver.url.md) | | string \| undefined | The base url for the server, including the basePath if set. For example, the value will be 'http://localhost:3000/api' if <code>basePath</code> is set to '/api'. | ## Methods | Method | Modifiers | Description | | --- | --- | --- | | [\_applyExpressSettings()](./rest.restserver._applyexpresssettings.md) | | Apply express settings. | | [\_handleHttpRequest(request, response)](./rest.restserver._handlehttprequest.md) | | | | [\_setupHandlerIfNeeded()](./rest.restserver._setuphandlerifneeded.md) | | | | [\_setupOASEnhancerIfNeeded()](./rest.restserver._setupoasenhancerifneeded.md) | | | | [\_setupOpenApiSpecEndpoints()](./rest.restserver._setupopenapispecendpoints.md) | | Mount /openapi.json, /openapi.yaml for specs and /swagger-ui, /explorer to redirect to externally hosted API explorer | | [\_setupRequestHandlerIfNeeded()](./rest.restserver._setuprequesthandlerifneeded.md) | | | | [\_unexpectedErrorHandler()](./rest.restserver._unexpectederrorhandler.md) | | Get an Express handler for unexpected errors | | [addOpenApiSpecEndpoint(path, form, router)](./rest.restserver.addopenapispecendpoint.md) | | Add a new non-controller endpoint hosting a form of the OpenAPI spec. | | [api(spec)](./rest.restserver.api.md) | | 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)](./rest.restserver.basepath.md) | | Configure the <code>basePath</code> for the rest server | | [bodyParser(bodyParserClass, address)](./rest.restserver.bodyparser.md) | | Bind a body parser to the server context | | [controller(controllerCtor)](./rest.restserver.controller.md) | | Register a controller class with this server. | | [exportOpenApiSpec(outFile, log)](./rest.restserver.exportopenapispec.md) | | Export the OpenAPI spec to the given json or yaml file | | [getApiSpec(requestContext)](./rest.restserver.getapispec.md) | | Get the OpenAPI specification describing the REST API provided by this application.<!-- -->This method merges operations (HTTP endpoints) from the following sources: - <code>app.api(spec)</code> - <code>app.controller(MyController)</code> - <code>app.route(route)</code> - <code>app.route('get', '/greet', operationSpec, MyController, 'greet')</code>If the optional <code>requestContext</code> is provided, then the <code>servers</code> list in the returned spec will be updated to work in that context. Specifically: 1. if <code>config.openApi.setServersFromRequest</code> is enabled, the servers list will be replaced with the context base url 2. Any <code>servers</code> entries with a path of <code>/</code> will have that path replaced with <code>requestContext.basePath</code> | | [handler(handlerFn)](./rest.restserver.handler.md) | | Configure a custom sequence function for handling incoming requests. | | [mountExpressRouter(basePath, router, spec)](./rest.restserver.mountexpressrouter.md) | | Mount an Express router to expose additional REST endpoints handled via legacy Express-based stack. | | [redirect(fromPath, toPathOrUrl, statusCode)](./rest.restserver.redirect.md) | | Register a route redirecting callers to a different URL. | | [route(verb, path, spec, controllerCtor, controllerFactory, methodName)](./rest.restserver.route.md) | | Register a new Controller-based route. | | [route(verb, path, spec, handler)](./rest.restserver.route_1.md) | | Register a new route invoking a handler function. | | [route(route)](./rest.restserver.route_2.md) | | Register a new generic route. | | [sequence(sequenceClass)](./rest.restserver.sequence.md) | | Configure a custom sequence class for handling incoming requests. | | [start()](./rest.restserver.start.md) | | Start this REST API's HTTP/HTTPS server. | | [static(path, rootDir, options)](./rest.restserver.static.md) | | Mount static assets to the REST server. See https://expressjs.com/en/4x/api.html\#express.static | | [stop()](./rest.restserver.stop.md) | | Stop this REST API's HTTP/HTTPS server. |