Skip to content

Commit

Permalink
Deploy strongloop/loopback.io to github.com/strongloop/loopback.io.gi…
Browse files Browse the repository at this point in the history
…t:gh-pages
  • Loading branch information
slnode committed Oct 9, 2018
1 parent 945192c commit 0a3f0e0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 5 deletions.
4 changes: 4 additions & 0 deletions _data/sidebars/lb4_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ children:
output: 'web, pdf'
children:

- title: 'Routing requests'
url: Routing-requests.html
output: 'web, pdf'

- title: 'Parsing requests'
url: Parsing-requests.html
output: 'web, pdf'
Expand Down
2 changes: 1 addition & 1 deletion pages/en/lb4/Repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ context.
```ts
import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = require('./redis.datasource.json');
import * as config from './redis.datasource.json';

export class RedisDataSource extends juggler.DataSource {
static dataSourceName = 'redis';
Expand Down
3 changes: 1 addition & 2 deletions pages/en/lb4/Routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ function greet(name: string) {
}

const app = new RestApplication();
const route = new Route('get', '/', spec, greet);
app.route(route); // attaches route to RestServer
app.route('get', '/', spec, greet); // attaches route to RestServer

app.start();
```
Expand Down
92 changes: 92 additions & 0 deletions pages/en/lb4/Routing-requests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
lang: en
title: 'Routing requests'
keywords: LoopBack 4.0, LoopBack 4
sidebar: lb4_sidebar
permalink: /doc/en/lb4/Routing-requests.html
---

## Routing Requests

This is an action in the default HTTP sequence. Its responsibility is to find a
route that can handle a given http request. By default, the `FindRoute` action
uses the `RoutingTable` from `@loopback/rest` to match requests against
registered routes including controller methods using `request.method` and
`request.path`. For example:

- GET /orders => OrderController.getOrders (`@get('/orders')`)
- GET /orders/123 => OrderController.getOrderById (`@get('/orders/{id}')`)
- GET /orders/count => OrderController.getOrderCount (`@get('/orders/count')`)
- POST /orders => OrderController.createOrder (`@post('/orders')`)

## Customize the `FindRoute` action

The `FindRoute` action is bound to `SequenceActions.FIND_ROUTE`
('rest.sequence.actions.findRoute') and injected into the default sequence.

To create your own `FindRoute` action, bind your implementation as follows:

```ts
const yourFindRoute: FindRoute = ...;
app.bind(SequenceActions.FIND_ROUTE).to(yourFindRoute);
```

## Customize the REST Router

Instead of rewriting `FindRoute` action completely, LoopBack 4 also allows you
to simply replace the `RestRouter` implementation.

The `@loopback/rest` module ships two built-in routers:

- TrieRouter: it keeps routes as a `trie` tree and uses traversal to match
`request` to routes based on the hierarchy of the path
- RegExpRouter: it keeps routes as an array and uses `path-to-regexp` to match
`request` to routes based on the path pattern

For both routers, routes without variables are optimized in a map so that any
requests matching to a fixed path can be resolved quickly.

By default, `@loopback/rest` uses `TrieRouter` as it performs better than
`RegExpRouter`. There is a simple benchmarking for `RegExpRouter` and
`TrieRouter` at
https://githhub.com/strongloop/loopback-next/benchmark/src/rest-routing/routing-table.ts.

To change the router for REST routing, we can bind the router class as follows:

```ts
import {RestBindings, RegExpRouter} from '@loopback/rest';
app.bind(RestBindings.ROUTER).toClass(RegExpRouter);
```

It's also possible to have your own implementation of `RestRouter` interface
below:

```ts
/**
* Interface for router implementation
*/
export interface RestRouter {
/**
* Add a route to the router
* @param route A route entry
*/
add(route: RouteEntry): boolean;

/**
* Find a matching route for the given http request
* @param request Http request
* @returns The resolved route, if not found, `undefined` is returned
*/
find(request: Request): ResolvedRoute | undefined;

/**
* List all routes
*/
list(): RouteEntry[];
}
```

See examples at:

- [TrieRouter](https://github.com/strongloop/loopback-next/tree/master/packages/rest/src/router/trie-router.ts)
- [RegExpRouter](https://github.com/strongloop/loopback-next/tree/master/packages/rest/src/router/regexp-router.ts)
3 changes: 1 addition & 2 deletions pages/en/lb4/Testing-your-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,7 @@ instance:

```ts
import {merge} from 'lodash';

const GEO_CODER_CONFIG = require('../src/datasources/geo.datasource.json');
import * as GEO_CODER_CONFIG from '../src/datasources/geo.datasource.json';

function givenGeoService() {
const config = merge({}, GEO_CODER_CONFIG, {
Expand Down
4 changes: 4 additions & 0 deletions pages/en/lb4/sidebars/lb4_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ children:
output: 'web, pdf'
children:

- title: 'Routing requests'
url: Routing-requests.html
output: 'web, pdf'

- title: 'Parsing requests'
url: Parsing-requests.html
output: 'web, pdf'
Expand Down

0 comments on commit 0a3f0e0

Please sign in to comment.