# sierra

> A service framework for Node.js.

Latest version **0.6.0-rc1.5** (published 2021-04-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install sierra
pnpm add sierra
yarn add sierra
bun add sierra
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0-rc1.5 |
| Published | 2021-04-23 |
| First published | 2016-06-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 136.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sean Johnson |
| Maintainers | sjohnsonaz |
| Keywords | service, framework, node, middleware, routing, router |

## Links

- npm: https://www.npmjs.com/package/sierra
- Repository: https://github.com/sjohnsonaz/sierra
- Homepage: https://github.com/sjohnsonaz/sierra#readme
- Issues: https://github.com/sjohnsonaz/sierra/issues
- npm.io page: https://npm.io/package/sierra

## Dependencies (3)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) 0.1.13
- [@cardboardrobots/pipeline](https://npm.io/package/@cardboardrobots/pipeline.md) 0.0.3
- [@cardboardrobots/console-style](https://npm.io/package/@cardboardrobots/console-style.md) 0.0.1

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.6.0-rc1.5 (latest) — 2021-04-23
- 0.6.0-rc1.4 — 2021-04-22
- 0.6.0-rc1.3 — 2021-04-21
- 0.6.0-rc1.2 — 2021-04-20
- 0.6.0-rc1.1 — 2021-04-20
- 0.6.0-rc1.0 — 2021-04-20
- 0.5.7 — 2020-10-14
- 0.5.6 — 2020-10-14
- 0.5.5 — 2020-10-14
- 0.5.4 — 2020-10-07
- 0.5.3 — 2020-10-01
- 0.5.2 — 2020-10-01
- 0.5.1 — 2020-08-27
- 0.5.0 — 2020-04-23
- 0.4.1 — 2020-04-08
- … 42 more at https://npm.io/package/sierra/versions

## README

# Sierra

![Node.js CI](https://github.com/sjohnsonaz/sierra/workflows/Node.js%20CI/badge.svg) [![npm version](https://badge.fury.io/js/sierra.svg)](https://badge.fury.io/js/sierra)

**Modern MVC support for your Node.js application.**

Sierra provides Promise based Middleware, Routing, and MVC style Controllers.

## Creating Application

Sierra uses a Middleware pipeline to process HTTP Requests.  To get up and running, create a new Sierra instance.

```` TypeScript
import Sierra from 'sierra';

let sierra = new Sierra();
````

Initialize Sierra builds all of the middleware and routes.

```` TypeScript
Sierra.prototype.init(): RequestHandler;
````

Now Sierra is ready to listen.  Start it by calling.

```` TypeScript
Sierra.prototype.listen(port: number): Promise<http.Server>;
````

## Creating Controllers

Sierra uses a routing system to respond to HTTP Requests.  The `pathname` of the Request is matched against a series of `RegExp` objects.

We generate these routes through `Controller` objects.  When defining a Controller, extend `Controller`, and specify routes with either the `@method` or `@route` decorator.

```` TypeScript
import { Controller, method } from 'sierra';

export default class TestController extends Controller {

    @method('get')
    async index() {
        return {
            pageName: 'index'
        };
    }
}
````

Sierra will build routes automatically based on the Controller's `Controller.base` property.  This can be set manually, or through the constructor.

```` TypeScript
class Controller {
    base: string;
    constructor(base?:string);
}
````

If no `Controller.base` is set, it will be generated from name of the Controller.  If the Controller's name ends with `Controller`, `Service`, or `Router`, the portion preceeding that will be used.

## Creating Routes

We can define routes on a Controller by marking methods with `@method` or `@route` decorators.  Only methods marked with these decorators will be used as routes.

For example:

```` TypeScript
@method('get')
async get() {
}

@route('post')
async post(context: Context, value: any) {
}
````

These two decorators are very similar.  First off, we have an HTTP method, here called a `Verb`.  This is 

```` TypeScript
enum Verb {
    All = 'all',
    Get = 'get',
    Post = 'post',
    Put = 'put',
    Delete = 'delete',
    Patch = 'patch',
    Options = 'options',
    Head = 'head'
}

function route<U extends IMiddleware<any, any>>(verb?: VerbType, name?: string | RegExp, pipeArgs: boolean = false);

function method<U extends Function>(verb?: VerbType, name?: string | RegExp);

function middleware<T extends IMiddleware<any, any>, U extends IMiddleware<any, any>>(middleware: T);
````

```` TypeScript
@method('post')
async post($body: Data) {
    return this.gateway.create($body);
}

@method('put', '/:id')
async put(id: string, $body: Data) {
    return this.gateway.update(id, $body);
}

@method('delete')
async delete(id: string) {
    return this.gateway.delete(id);
}
````

## Example Service

```` TypeScript
export default class DataController extends Controller {
    gateway: Gateway<Data>;

    constructor(gateway: Gateway<Data>) {
        super('data');
        this.gateway = gateway;
    }

    @method('get', '/')
    async list(page: number, pageSize: number) {
        return await this.gateway.find({
            page: page,
            pageSize: pageSize
        });
    }

    @method('get', '/:id')
    async get(id: string) {
        return await this.gateway.get(id);
    }

    @method('post')
    async post($body: Data) {
        return this.gateway.create($body);
    }

    @method('put', '/:id')
    async put(id: string, $body: Data) {
        return this.gateway.update(id, $body);
    }

    @method('delete')
    async delete(id: string) {
        return this.gateway.delete(id);
    }
}
````

## Add Controllers and Middleware

Before Sierra is initialized, call:

```` TypeScript
session.use(async (context: Context, value: any) => {
    return true;
});
````

```` TypeScript
session.addController(new ExampleController());
````

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