# @fulminate/router

> Class-based views (controllers) for using with Express and TypeScript.

Latest version **2.0.3** (published 2017-07-31) · MIT license · 18 weekly downloads

## Install

```sh
npm install @fulminate/router
pnpm add @fulminate/router
yarn add @fulminate/router
bun add @fulminate/router
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.3 |
| Published | 2017-07-31 |
| First published | 2017-07-04 |
| Weekly downloads | 18 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Sergey Orlov |
| Maintainers | fulminate |

## Links

- npm: https://www.npmjs.com/package/@fulminate/router
- npm.io page: https://npm.io/package/@fulminate/router

## Dependencies (7)

- [cookie](https://npm.io/package/cookie.md) ^0.3.1
- [morgan](https://npm.io/package/morgan.md) ^1.8.2
- [typedi](https://npm.io/package/typedi.md) ^0.5.2
- [express](https://npm.io/package/express.md) ^4.15.3
- [body-parser](https://npm.io/package/body-parser.md) ^1.17.2
- [cookie-parser](https://npm.io/package/cookie-parser.md) ^1.4.3
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.10

## Recent versions

- 2.0.3 (latest) — 2017-07-31
- 2.0.2 — 2017-07-31
- 2.0.1 — 2017-07-31
- 2.0.0 — 2017-07-31
- 1.1.6 — 2017-07-07
- 1.1.5 — 2017-07-07
- 1.1.4 — 2017-07-07
- 1.1.3 — 2017-07-07
- 1.1.2 — 2017-07-07
- 1.1.1 — 2017-07-07
- 1.1.0 — 2017-07-06
- 1.0.4 — 2017-07-05
- 1.0.3 — 2017-07-05
- 1.0.1 — 2017-07-04
- 1.0.0 — 2017-07-04

## README

# Fulminate Class-Based View Router

This package introduces class-based views (controllers) for Express.js. Designed to be used with TypeScript.
Heavily inspired by Django's class based views.
This is a full revision of previously published Fulminate Router. To install current version:

`npm i --save @fulminate/router`

To use the deprecated version, install it with:

`npm i @fulminate/router@1.1.6`

## Migration guide (1.x.x to 2.x.x)

No migration. Its a 100% redone package.

## Example of use:

You first need to wire up the server. Fulminate class-based views wrap up Express server. If you've already started
working with Express, you can simply inject your code into Fulminate CBV without rewriting.

### Creating brand-new express server with Fulminate Router

```typescript
import * as morgan from 'morgan';
import { FulminateRouter } from '@fulminate/router';

const fS = new FulminateRouter();
// Provide paths and names of your Fulminate CBV files to make the router automatically require them.
// NOTE: You can provide strings, arrays of strings, regexes and arrays of regexes.
fS.registerViews(__dirname, [ /\/*.controller.js$/ ]);
fS.wireUp();

const server = fS.getServer();

// Add any required dependencies
// NOTE: Body and cookie parsers are already injected into Fulminate Router.
server.use(morgan('dev'));

// Set up view engine
server.set('view engine', 'ejs')
  .set('views', './')
  .engine('.html', require('ejs').renderFile)
;

server.listen(3000);
```

### Injecting existing Express server into Fulminate Router

```typescript
import * as express from 'express';
import * as morgan from 'morgan';
import { FulminateRouter } from '@fulminate/router';

const server = express();

server.use(morgan('dev'));

// Set up view engine
server.set('view engine', 'ejs')
  .set('views', './')
  .engine('.html', require('ejs').renderFile)
;

const fS = new FulminateRouter(server);
fS.registerViews(__dirname, [ /\/*.controller.js$/ ]);
fS.wireUp();

const server = fS.getServer();

// NOTE: You need to listen to the server taken from Fulminate Router.
fS.getServer().listen(3000);
```

### Examples of view classes

```typescript
import { Body, ClassBasedView, GetCookies, GetHeaders, QueryParams, RequestParams, SetCookie, SetHeader } from '@fulminate/class-based-view';

// View marked 'asJson: true' will return JSON.
@ClassBasedView({
  route: '/',
  asJson: true,
})
// Set response headers
@SetHeader({ key: 'X-Powered-By', value: 'Someone_Else' })
@SetHeader({ key: 'Authorization', value: 'ThisIsToken' })
export class TestView {
  // Decorated variables will not be listed in the response object and are meant to be used internally.
  @GetCookies()
  private _cookies: object;
  
  // To visually simplify the code, use 'private' for non-enumerable values and 'public' for enumerable ones.
  public title = 'TestView works!';
  
  // To make decorated variables enumerable, simply use a getter.
  // NOTE: You need to target at least 'es5' in tsconfig to be able to use getters.
  public get cookies(): object {
    return this._cookies;
  }
}

// Provide the route, marking request parameters with colons (':').
// NOTE: Template logic only works if you set up view engine.
@ClassBasedView({
  route: '/:param',
  template: 'index.html',
})
export class Test2View {
  // Set cookies (name of the variable is used as a cookie name and its value is used as cookie value).
  @SetCookie() cookieTheAnswer = '42';
  @SetCookie() whatsHerName = 'noname';
  
  public title: string;

  // Get access to request headers
  @GetHeaders()
  private headers: object;
  
  // Get query params
  // NOTE: Providing 'required' option to decorators will cause an error if the required data does not exist on request object.
  @QueryParams({ required: true })
  private queryParams: object;
  
  // Explicitly define which cookie or cookies are required
  @GetCookies({ required: true, explicitly: [ 'cookieTheAnswer' ]})
  private cookies: object;
  
  // Get request params
  // NOTE: Request params must also be present in the route option of @ClassBasedView decorator.
  @RequestParams()
  private requestParams: object;

  // Adjust values in the constructor.
  public constructor() {
    this.title = 'Test2View works!'
  }
}

// Define HTTP methods other than GET.
// NOTE: case-insensitive.
@ClassBasedView({
  route: '/',
  method: 'POST',
  asJson: true,
})
export class TestPostView {
  public name: string = "TestPostView works!";

  // Get the body of the request
  @Body()
  private body;
}
```

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