Skip to content

Commit

Permalink
fix(docs): extract route how-to
Browse files Browse the repository at this point in the history
Signed-off-by: Diana Lau <[email protected]>
  • Loading branch information
dhmlau committed Oct 5, 2020
1 parent 0dbdd01 commit 3c95375
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 83 deletions.
95 changes: 95 additions & 0 deletions docs/site/Customizing-routes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
lang: en
title: 'Customizing Routes'
keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI, Express
sidebar: lb4_sidebar
permalink: /doc/en/lb4/Customizing-routes.html
---

In addition to defining routes at the Controller and operation level, you can
also customize them at the application level.

## Implementing HTTP redirects

Both `RestServer` and `RestApplication` classes provide API for registering
routes that will redirect clients to a given URL.

Example use:

{% include code-caption.html content="src/application.ts" %}

```ts
import {RestApplication} from '@loopback/rest';

export class MyApplication extends RestApplication {
constructor(options: ApplicationConfig = {}) {
super(options);

// Use the default status code 303 See Other
this.redirect('/', '/home');

// Specify a custom status code 301 Moved Permanently
this.redirect('/stats', '/status', 301);
}
}
```

## Mounting an Express Router

If you have an existing [Express](https://expressjs.com/) application that you
want to use with LoopBack 4, you can mount the Express application on top of a
LoopBack 4 application. This way you can mix and match both frameworks, while
using LoopBack as the host. You can also do the opposite and use Express as the
host by mounting LoopBack 4 REST API on an Express application. See
[Creating an Express Application with LoopBack REST API](express-with-lb4-rest-tutorial.md)
for the tutorial.

Mounting an Express router on a LoopBack 4 application can be done using the
`mountExpressRouter` function provided by both
[`RestApplication`](https://loopback.io/doc/en/lb4/apidocs.rest.restapplication.html)
and [`RestServer`](https://loopback.io/doc/en/lb4/apidocs.rest.restserver.html).

Example use:

{% include note.html content="
Make sure [express](https://www.npmjs.com/package/express) is installed.
" %}

{% include code-caption.html content="src/express-app.ts" %}

```ts
import {Request, Response} from 'express';
import express from 'express';

const legacyApp = express();

// your existing Express routes
legacyApp.get('/pug', function (_req: Request, res: Response) {
res.send('Pug!');
});

export {legacyApp};
```

{% include code-caption.html content="src/application.ts" %}

```ts
import {RestApplication} from '@loopback/rest';

const {legacyApp} = require('./express-app');

const openApiSpecForLegacyApp: RouterSpec = {
// insert your spec here, your 'paths', 'components', and 'tags' will be used
};

class MyApplication extends RestApplication {
constructor(/* ... */) {
// ...

this.mountExpressRouter('/dogs', legacyApp, openApiSpecForLegacyApp);
}
}
```

Any routes you define in your `legacyApp` will be mounted on top of the `/dogs`
base path, e.g. if you visit the `/dogs/pug` endpoint, you'll see `Pug!`.
86 changes: 3 additions & 83 deletions docs/site/Route.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,87 +201,7 @@ class MySequence extends DefaultSequence {
}
```

## Implementing HTTP redirects
## Related guides

Both `RestServer` and `RestApplication` classes provide API for registering
routes that will redirect clients to a given URL.

Example use:

{% include code-caption.html content="src/application.ts" %}

```ts
import {RestApplication} from '@loopback/rest';

export class MyApplication extends RestApplication {
constructor(options: ApplicationConfig = {}) {
super(options);

// Use the default status code 303 See Other
this.redirect('/', '/home');

// Specify a custom status code 301 Moved Permanently
this.redirect('/stats', '/status', 301);
}
}
```

## Mounting an Express Router

If you have an existing [Express](https://expressjs.com/) application that you
want to use with LoopBack 4, you can mount the Express application on top of a
LoopBack 4 application. This way you can mix and match both frameworks, while
using LoopBack as the host. You can also do the opposite and use Express as the
host by mounting LoopBack 4 REST API on an Express application. See
[Creating an Express Application with LoopBack REST API](express-with-lb4-rest-tutorial.md)
for the tutorial.

Mounting an Express router on a LoopBack 4 application can be done using the
`mountExpressRouter` function provided by both
[`RestApplication`](https://loopback.io/doc/en/lb4/apidocs.rest.restapplication.html)
and [`RestServer`](https://loopback.io/doc/en/lb4/apidocs.rest.restserver.html).

Example use:

{% include note.html content="
Make sure [express](https://www.npmjs.com/package/express) is installed.
" %}

{% include code-caption.html content="src/express-app.ts" %}

```ts
import {Request, Response} from 'express';
import express from 'express';

const legacyApp = express();

// your existing Express routes
legacyApp.get('/pug', function (_req: Request, res: Response) {
res.send('Pug!');
});

export {legacyApp};
```

{% include code-caption.html content="src/application.ts" %}

```ts
import {RestApplication} from '@loopback/rest';

const legacyApp = require('./express-app').legacyApp;

const openApiSpecForLegacyApp: RouterSpec = {
// insert your spec here, your 'paths', 'components', and 'tags' will be used
};

class MyApplication extends RestApplication {
constructor(/* ... */) {
// ...

this.mountExpressRouter('/dogs', legacyApp, openApiSpecForLegacyApp);
}
}
```

Any routes you define in your `legacyApp` will be mounted on top of the `/dogs`
base path, e.g. if you visit the `/dogs/pug` endpoint, you'll see `Pug!`.
- [Implementing HTTP redirects](Customizing-routes.md#implementing-http-redirects)
- [Mounting an Express Router](Customizing-routes.md#mounting-an-express-router)
4 changes: 4 additions & 0 deletions docs/site/sidebars/lb4_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ children:
url: Customizing-how-openapi-spec-is-served.html
output: 'web, pdf'

- title: 'Customizing routes'
url: Customizing-routes.html
output: 'web, pdf'

- title: 'Dynamically adding models, repositories, and controllers'
url: Dynamic-models-repositories-controllers.html
output: 'web, pdf'
Expand Down

0 comments on commit 3c95375

Please sign in to comment.