Skip to content

Commit

Permalink
chore(rest): review - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Dec 3, 2018
1 parent a99ad6a commit 7e5b5ab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
30 changes: 28 additions & 2 deletions docs/site/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,38 @@ export async function main() {
For a complete list of CORS options, see
https://github.com/expressjs/cors#configuration-options.

### Configure the Base Path

Sometime it's desirable to expose REST endpoints using a base path, such as
`/api`. The base path can be set as part of the RestServer configuration.

```ts
const app = new RestApplication({
rest: {
basePath: '/api',
},
});
```

The `RestApplication` and `RestServer` both provide a `basePath()` API:

```ts
const app: RestApplication;
// ...
app.basePath('/api');
```

With the `basePath`, all REST APIs and static assets are served on URLs starting
with the base path.

### `rest` options

| Property | Type | Purpose |
| ----------- | ------------------- | --------------------------------------------------------------------------------------------------------- |
| port | number | Specify the port on which the RestServer will listen for traffic. |
| protocol | string (http/https) | Specify the protocol on which the RestServer will listen for traffic. |
| host | string | Specify the hostname or ip address on which the RestServer will listen for traffic. |
| port | number | Specify the port on which the RestServer listens for traffic. |
| protocol | string (http/https) | Specify the protocol on which the RestServer listens for traffic. |
| basePath | string | Specify the base path that RestServer exposes http endpoints. |
| key | string | Specify the SSL private key for https. |
| cert | string | Specify the SSL certificate for https. |
| cors | CorsOptions | Specify the CORS options. |
Expand Down
5 changes: 3 additions & 2 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,9 @@ export class RestServer extends Context implements Server, HttpServerLike {

if (specObj.servers && this._basePath) {
for (const s of specObj.servers) {
if (s.url.startsWith('/')) {
s.url = s.url === '/' ? this._basePath : this._basePath + s.url;
// Update the default server url to honor `basePath`
if (s.url === '/') {
s.url = this._basePath;
}
}
}
Expand Down

0 comments on commit 7e5b5ab

Please sign in to comment.