Skip to content

Commit

Permalink
Merge b918f0d into 1928b36
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Oct 7, 2019
2 parents 1928b36 + b918f0d commit 3600e13
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
29 changes: 15 additions & 14 deletions docs/site/Server.md
Expand Up @@ -290,20 +290,21 @@ for more details.

### `rest` options

| Property | Type | Purpose |
| ----------------- | ------------------------ | --------------------------------------------------------------------------------------------------------- |
| 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. |
| sequence | SequenceHandler | Use a custom SequenceHandler to change the behavior of the RestServer for the request-response lifecycle. |
| openApiSpec | OpenApiSpecOptions | Customize how OpenAPI spec is served |
| apiExplorer | ApiExplorerOptions | Customize how API explorer is served |
| requestBodyParser | RequestBodyParserOptions | Customize how request body is parsed |
| router | RouterOptions | Customize how trailing slashes are used for routing |
| Property | Type | Purpose |
| ----------------- | ------------------------- | --------------------------------------------------------------------------------------------------------- |
| 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. |
| sequence | SequenceHandler | Use a custom SequenceHandler to change the behavior of the RestServer for the request-response lifecycle. |
| openApiSpec | OpenApiSpecOptions | Customize how OpenAPI spec is served |
| apiExplorer | ApiExplorerOptions | Customize how API explorer is served |
| requestBodyParser | RequestBodyParserOptions | Customize how request body is parsed |
| router | RouterOptions | Customize how trailing slashes are used for routing |
| listenOnStart | boolean (default to true) | Control if the server should listen on http/https when it's started |

## Add servers to application instance

Expand Down
47 changes: 44 additions & 3 deletions docs/site/express-with-lb4-rest-tutorial.md
Expand Up @@ -234,11 +234,21 @@ export class ExpressServer {
await this.lbApp.boot();
}

async start() {
public async start() {
await this.lbApp.start();
const port = this.lbApp.restServer.config.port || 3000;
const host = this.lbApp.restServer.config.host || '127.0.0.1';
const server = this.app.listen(port, host);
await pEvent(server, 'listening');
this.server = this.app.listen(port, host);
await pEvent(this.server, 'listening');
}

// For testing purposes
public async stop() {
if (!this.server) return;
await this.lbApp.stop();
this.server.close();
await pEvent(this.server, 'close');
this.server = undefined;
}
}
```
Expand All @@ -262,6 +272,37 @@ export async function main(options: ApplicationConfig = {}) {
}
```

{% include code-caption.html content="index.js" %}

```js
const application = require('./dist');

module.exports = application;

if (require.main === module) {
// Run the application
const config = {
rest: {
port: +process.env.PORT || 3000,
host: process.env.HOST || 'localhost',
openApiSpec: {
// useful when used with OpenAPI-to-GraphQL to locate your application
setServersFromRequest: true,
},
// Use the LB4 application as a route. It should not be listening.
listenOnStart: false,
},
};
application.main(config).catch(err => {
console.error('Cannot start the application.', err);
process.exit(1);
});
}
```

Please note `listenOnStart` is set to `false` to instruct the LB4 application is
not listening on HTTP when it's started as the Express server will be listening.

Now let's start the application and visit <http://127.0.0.1:3000>:

```sh
Expand Down

0 comments on commit 3600e13

Please sign in to comment.