@loopback/docs
Version:
Documentation for LoopBack 4
94 lines (71 loc) • 6.07 kB
Markdown
---
lang: en
title: 'API docs: rest.restserver'
keywords: LoopBack 4.0, LoopBack 4
sidebar: lb4_sidebar
permalink: /doc/en/lb4/apidocs.rest.restserver.html
---
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [@loopback/rest](./rest.md) > [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 Context implements Server, HttpServerLike
```
## 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) | | <code>express.Application</code> | |
| [\_httpHandler](./rest.restserver._httphandler.md) | | <code>HttpHandler</code> | |
| [\_httpServer](./rest.restserver._httpserver.md) | | <code>HttpServer | undefined</code> | |
| [\_requestHandler](./rest.restserver._requesthandler.md) | | <code>HttpRequestListener</code> | Handle incoming HTTP(S) request by invoking the corresponding Controller method via the configured Sequence. |
| [config](./rest.restserver.config.md) | | <code>RestServerResolvedConfig</code> | |
| [httpHandler](./rest.restserver.httphandler.md) | | <code>HttpHandler</code> | |
| [listening](./rest.restserver.listening.md) | | <code>boolean</code> | |
| [requestHandler](./rest.restserver.requesthandler.md) | | <code>HttpRequestListener</code> | |
| [rootUrl](./rest.restserver.rooturl.md) | | <code>string | undefined</code> | 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) | | <code>string | undefined</code> | 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) | | |
| [\_onUnhandledError(req, res, err)](./rest.restserver._onunhandlederror.md) | | |
| [\_setupHandlerIfNeeded()](./rest.restserver._setuphandlerifneeded.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) | | |
| [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. |
| [getApiSpec()](./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> |
| [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(value)](./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. |
## 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');
```