# @nestcloud/gateway

> gateway module for nestcloud

Latest version **0.3.17** (published 2019-06-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nestcloud/gateway
pnpm add @nestcloud/gateway
yarn add @nestcloud/gateway
bun add @nestcloud/gateway
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.17 |
| Published | 2019-06-28 |
| First published | 2019-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 28.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 419 |
| Author | Miaowing |
| Maintainers | zfeng |

## Links

- npm: https://www.npmjs.com/package/@nestcloud/gateway
- Repository: https://github.com/nest-cloud/nestcloud
- Homepage: https://github.com/nest-cloud/nestcloud#readme
- Issues: https://github.com/nest-cloud/nestcloud/issues
- npm.io page: https://npm.io/package/@nestcloud/gateway

## Dependencies (5)

- [http-proxy](https://npm.io/package/http-proxy.md) ^1.17.0
- [ratelimiter](https://npm.io/package/ratelimiter.md) ^3.3.0
- [@nestcloud/boot](https://npm.io/package/@nestcloud/boot.md) ^0.3.17
- [@nestcloud/redis](https://npm.io/package/@nestcloud/redis.md) ^0.3.17
- [@nestcloud/consul-config](https://npm.io/package/@nestcloud/consul-config.md) ^0.3.17

## Recent versions

- 0.3.17 (latest) — 2019-06-28
- 0.3.16-0 (test) — 2019-06-14
- 0.2.4 (next) — 2019-04-26
- 0.3.16 — 2019-06-28
- 0.3.15 — 2019-06-14
- 0.3.14 — 2019-06-14
- 0.3.14-2 — 2019-06-13
- 0.3.14-1 — 2019-06-13
- 0.3.14-0 — 2019-06-13
- 0.3.13 — 2019-06-13
- 0.3.12 — 2019-06-11
- 0.3.10 — 2019-06-05
- 0.3.9 — 2019-06-05
- 0.3.9-0 — 2019-06-05
- 0.3.8 — 2019-06-05
- … 33 more at https://npm.io/package/@nestcloud/gateway/versions

## README

[travis-image]: https://api.travis-ci.org/nest-cloud/nestcloud.svg?branch=master
[travis-url]: https://travis-ci.org/nest-cloud/nestcloud
[linux-image]: https://img.shields.io/travis/nest-cloud/nestcloud/master.svg?label=linux
[linux-url]: https://travis-ci.org/nest-cloud/nestcloud

# NestCloud - Gateway

<p align="center">
    <a href="https://www.npmjs.com/~nestcloud" target="_blank"><img src="https://img.shields.io/npm/v/@nestcloud/core.svg" alt="NPM Version"/></a>
    <a href="https://www.npmjs.com/~nestcloud" target="_blank"><img src="https://img.shields.io/npm/l/@nestcloud/core.svg" alt="Package License"/></a>
    <a href="https://www.npmjs.com/~nestcloud" target="_blank"><img src="https://img.shields.io/npm/dm/@nestcloud/core.svg" alt="NPM Downloads"/></a>
    <a href="https://travis-ci.org/nest-cloud/nestcloud" target="_blank"><img src="https://travis-ci.org/nest-cloud/nestcloud.svg?branch=master" alt="Travis"/></a>
    <a href="https://travis-ci.org/nest-cloud/nestcloud" target="_blank"><img src="https://img.shields.io/travis/nest-cloud/nestcloud/master.svg?label=linux" alt="Linux"/></a>
    <a href="https://coveralls.io/github/nest-cloud/nestcloud?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nest-cloud/nestcloud/badge.svg?branch=master" alt="Coverage"/></a>
</p>

## Description

The gateway module for nestcloud.

[中文文档](https://github.com/nest-cloud/nestcloud/blob/master/docs/gateway.md)

## Installation

```bash
$ npm install --save @nestcloud/gateway
```

## Notification

You should not use body parser middleware when use this module or the post request will hang.

## Quick Start

### Register Module

```typescript
import { Module } from '@nestjs/common';
import { NEST_BOOT } from '@nestcloud/common';
import { GatewayModule } from "@nestcloud/gateway";

@Module({
    imports: [
        GatewayModule.register({dependencies: [NEST_BOOT]}),
    ]
})
export class AppModule {
}
```

### Configurations

```yaml
gateway:
  routes:
    - id: user
      uri: lb://nestcloud-user-service
    - id: pay
      uri: https://example.com/pay
```

## Usage

```typescript
import { All, Controller, Param, Req, Res } from "@nestjs/common";
import { Request, Response } from 'express';
import { Gateway, InjectGateway } from "@nestcloud/gateway";

@Controller('/gateway/:service')
export class GatewayController {
    constructor(
        @InjectGateway() private readonly gateway: Gateway,
    ) {
    }

    @All()
    do(@Req() req: Request, @Res() res: Response, @Param('service') id) {
        this.gateway.forward(req, res, id);
    }
    
    private updatRoutes() {
        const routes = [{id: 'example', uri: 'lb://example-service'}];
        this.gateway.updateRoutes(routes);
    }
}
```

## API

### class GatewayModule

#### static register\(options: IGatewayOptions = {}, proxy?: IProxyOptions\): DynamicModule

Register gateway module.

| field | type | description |
| :--- | :--- | :--- |
| options.dependencies | string[] | NEST_BOOT or NEST_CONSUL_CONFIG |
| options.routes | IRoute[] | routes of gateway |
| proxy | IProxyOptions | please see http-proxy doc for detail |

### class Gateway

#### updateRoutes(routes: IRoute[], sync: boolean = true): void

Update gateway routes.

## TODO

filter support


## Stay in touch

- Author - [NestCloud](https://github.com/nest-cloud)

## License

  NestCloud is [MIT licensed](LICENSE).

---
_Source: https://npm.io/package/@nestcloud/gateway · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
