# morest-express

> Elegante, decorators, express framework

Latest version **1.1.2** (published 2021-01-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install morest-express
pnpm add morest-express
yarn add morest-express
bun add morest-express
```

Provides the command `morest-express`.

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2021-01-05 |
| First published | 2020-12-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 0.10.0 |
| Dependencies | 5 |
| Unpacked size | 49.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alexandre Longordo |
| Maintainers | rawz06 |
| Keywords | morest, decorators, express, framework, web, http, rest, restful, router, app, api, middleware |

## Links

- npm: https://www.npmjs.com/package/morest-express
- Repository: https://github.com/AlexandreLon/morest-express
- Homepage: https://github.com/AlexandreLon/morest-express#readme
- Issues: https://github.com/AlexandreLon/morest-express/issues
- npm.io page: https://npm.io/package/morest-express

## Dependencies (5)

- [arg](https://npm.io/package/arg.md) ^5.0.0
- [ejs](https://npm.io/package/ejs.md) ^3.1.5
- [express](https://npm.io/package/express.md) ^4.17.1
- [typescript](https://npm.io/package/typescript.md) ^4.1.3
- [@types/express](https://npm.io/package/@types/express.md) ^4.17.9

## 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

- 1.1.2 (latest) — 2021-01-05
- 1.1.1 — 2021-01-04
- 1.1.0 — 2020-12-31
- 1.0.1 — 2020-12-30
- 1.0.0 — 2020-12-30

## README

# ![morest](https://user-images.githubusercontent.com/43060105/103370527-50d7c080-4acd-11eb-9607-68e671115795.png)


Morest is an abstraction of express to use it with decorators. It was created to have a better readability of code.

WARNING

This is a first version of implementation and all features to corresponding with express are not implemented

## Features

* Create class controller
* Create all routes (get, put, post, patch, delete)
* Possibility to add middleware
* Support of ejs render

## Installation

This is a [Node.js](https://nodejs.org/en/) module.

Before installing, [download and install Node.js](https://nodejs.org/en/download/).
Node.js 0.10 or higher is required.

```bash
$ npm install morest-express
```

or

```bash
$ yarn add morest-express
```

## How to use

### Hello World example

```ts
import Morest, {GetMapping, Controller, Request, Response} from 'morest-express'

@Controller('')
class IndexController {
​
    @GetMapping('/')
    index(req: Request, res: Response) {
        res.send("Hello World!")
    }
}
​
const morest = new Morest()
​
morest.run(3000, () => {
    console.log("Listening at 3000")
})

```

### Add a middleware

Create a class middleware

```ts
import { MiddlewareModel, Request, Response, NextFunction } from 'morest-express';

class HelloMiddleware implements MiddlewareModel {

    run(req: Request, res: Response, next: NextFunction) {
        console.log("hello i'm a middleware")
        next()
    }

}
```

Add it on your controller 

```ts
import Morest, {MiddlewareController, GetMapping, Controller, Request, Response} from 'morest-express'

@MiddlewareController(HelloMiddleware)
@Controller('/api')
class IndexController {
​
    @GetMapping('/')
    index(req: Request, res: Response) {
        res.send("Hello World!")
    }
}
```

or a specific route

```ts
import Morest, {MiddlewareRoute, GetMapping, Controller, Request, Response} from 'morest-express'

@Controller('/api')
class IndexController {
​
    @MiddlewareRoute(HelloMiddleware)
    @GetMapping('/')
    index(req: Request, res: Response) {
        res.send("Hello World!")
    }
}
```

or global middleware for all route 

```ts
const morest = new Morest()

morest.useMiddleware(HelloMiddleware)

morest.run(3000, () => {
    console.log("Listening at 3000")
})

```

Possibility to add other middle like json-parser or cors : 

```ts
const morest = new Morest()

morest.use(bodyParser.json())
morest.use(cors())

morest.run(3000, () => {
    console.log("Listening at 3000")
})

```

You can add params to pass it inside Middleware like this

Middleware : 

```ts
import { MiddlewareModel, Request, Response, NextFunction } from 'morest-express';

class HelloMiddleware implements MiddlewareModel {

    run(req: Request, res: Response, next: NextFunction) {
        console.log(this.params) // {foo: bar}
        const params = this.params as {foo: string}
        console.log(params.foo) // "bar"
        next()
    }

}
```

Route : 

```ts
import Morest, {MiddlewareRoute, GetMapping, Controller, Request, Response} from 'morest-express'

@Controller('/api')
class IndexController {
​
    @MiddlewareRoute(HelloMiddleware, {foo: "bar"})
    @GetMapping('/')
    index(req: Request, res: Response) {
        res.send("Hello World!")
    }
}
```


### List of decorators

| Decorators | Usage | Description |
| ------ | ----------- | ----------- | 
| Controller   | @Controller(path) | Set a class as a controller. Use path to have a base path for each route inside a controller
| RouteMapping | @RouteMapping(method, path) | Set a function as route. The first parameter is the method and the second, the path. The path is optionnal and default value is '/'
| GetMapping    | @GetMapping(path) | Set a function as route get. The path is optionnal and default value is '/'
| PostMapping    | @PostMapping(path) | Set a function as route post. The path is optionnal and default value is '/'
| PutMapping    | @PutMapping(path) | Set a function as route put. The path is optionnal and default value is '/'
| PatchMapping    | @PatchMapping(path) | Set a function as route patch. The path is optionnal and default value is '/'
| DeleteMapping    | @DeleteMapping(path) | Set a function as route delete. The path is optionnal and default value is '/'
| MiddlewareController | @MiddlewareController(Middleware, params) | Set a middle on the controller, each route will have a middleware. The middleware must be a class inheritance MiddlewareModel. Params is list of params will be accessible by `this.params` from Middleware class 
| MiddlewareRoute | @MiddlewareRoute(Middleware, params) | Set a middle on the route. The middleware must be a class inheritance MiddlewareModel. Params is list of params will be accessible by `this.params` from Middleware class 

### Morest-cli

You can also use the cli to generate middleware and controller. 

#### To generate a controller : 

```bash
$ npx morest-express --controller <path> 
```

You can add a `--resources` flag to generate also all resources route

```bash
$ npx morest-express --controller <path> --resources
```

| Method | URI         | Action      | 
| ------ | ----------- | ----------- | 
| GET    | /           | index       | 
| GET    | /:id        | show        | 
| GET    | /create     | create      | 
| POST   | /           | store       | 
| GET    | /:id/edit   | edit        | 
| PUT    | /:id        | update      | 
| DELETE | /:id        | delete      | 

#### To generate a middleware : 

```bash
$ npx morest-express --middleware <path> 
```

#### Path

The path must be like `path.of.the.file`

Will be generate :
- `./controllers/path/of/the/FileController.ts` for controllers. The route will be `http://localhost:3000/path/of/the/file`
- `./middleswares/path/of/the/FileMiddleware.ts` for middlewares 

You can use --src to change the source directory, by default it's `./src` and with --folder too you can specify other folder 

```bash
$ npx morest-express --middleware path.of.file --src
```

Will be generate at `./src/middleswares/path/of/the/FileMiddleware.ts`

```bash
$ npx morest-express --middleware path.of.file --src --folder code
```

Will be generate at `./code/middleswares/path/of/the/FileMiddleware.ts`

### EJS 

To use EJS you can add this on your app.ts

```ts
const morest = new Morest()

morest.set("view engine", "ejs");
morest.set("views", path.join(root.path, 'views'));

morest.run(3000, () => {
    console.log("Listening at 3000")
})
```

And use it like express. 

## License

[MIT](LICENSE)

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