# express-methods

> This project is build in TypeScript, demonstrates standard usage of express framework with advance custom request methods and error generators

Latest version **1.1.4** (published 2019-12-31) · ISC license · 0 weekly downloads

## Install

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

## 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 | 1.1.4 |
| Published | 2019-12-31 |
| First published | 2019-12-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | 8.11.1 |
| Dependencies | 5 |
| Unpacked size | 11.1 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Maintainers | rahultagadghar |

## Links

- npm: https://www.npmjs.com/package/express-methods
- npm.io page: https://npm.io/package/express-methods

## Dependencies (5)

- [dotenv](https://npm.io/package/dotenv.md) ^8.2.0
- [body-parser](https://npm.io/package/body-parser.md) ^1.19.0
- [http-errors](https://npm.io/package/http-errors.md) ^1.7.3
- [class-validator](https://npm.io/package/class-validator.md) ^0.11.0
- [class-transformer](https://npm.io/package/class-transformer.md) ^0.2.3

## Recent versions

- 1.1.4 (latest) — 2019-12-31
- 1.1.3 — 2019-12-31
- 1.1.2 — 2019-12-31
- 1.1.1 — 2019-12-31
- 1.1.0 — 2019-12-31
- 1.0.9 — 2019-12-31
- 1.0.8 — 2019-12-31
- 1.0.7 — 2019-12-31
- 1.0.6 — 2019-12-31
- 1.0.5 — 2019-12-31
- 1.0.4 — 2019-12-31
- 1.0.2 — 2019-12-31
- 1.0.1 — 2019-12-31
- 1.0.0 — 2019-12-31

## README

# Express Methods

This project is build in TypeScript, demonstrates standard usage of express framework with advance custom request methods and error generators

**Run application in Production mode**

```npm
    npm start
```

**Run application in Development mode** [ Configured with Nodemon ]

```npm
    npm run dev
```

## Finish Method

Finish method makes use of send method under the hood, finish method allows to send http response in a uniform pattern for all the APIS including success and failure response

```ts
const attachFinishMethod = (req: Request, res: ExpressResponse, next) => {
  let defaultHttpStatus = 200;

  if (req.method !== httpMethod.GET) {
    defaultHttpStatus = 201;
  }

  res.finish = (data = {}, message = "", httpStatus = defaultHttpStatus, errors = {}) => {
    res.status(httpStatus).send({
      message,
      data,
      errors
    });
  };
  next();
};
```

Make sure to add this middleware on highest scope of application, this will make sure to attach finish method to every request

```ts
app.use(attachFinishMethod);
```

Example for usage of finish method

```ts
app.get("/", (req, res: ExpressResponse, next) => {
  try {
    const result = {
      isUser: true
    };
    res.finish(result, "Fetched mock user info!");
  } catch (error) {
    next(error);
  }
});
```

# Error Generators

ExpressError is build on top of Error constructor it adds ability to send custom http message and status code

```ts
throw new ExpressError("Its Forbidden", 403);
```

```ts
throw new httpError.Forbidden();
```

Make sure to place this middleware at the end of scope, this middleware is compatible with both the error generators shown above

```ts
app.use(expressErrorHandler);
```

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