# @forrestjs/service-express

> ForrestJS service which helps setting up an ExpressJS server.

Latest version **3.19.0** (published 2020-10-03) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @forrestjs/service-express
pnpm add @forrestjs/service-express
yarn add @forrestjs/service-express
bun add @forrestjs/service-express
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 3.19.0 |
| Published | 2020-10-03 |
| First published | 2019-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 13.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 43 |
| Author | Marco Pegoraro |
| Maintainers | marcopeg |
| Keywords | forrestjs, service, express, server |

## Links

- npm: https://www.npmjs.com/package/@forrestjs/service-express
- Repository: https://github.com/forrestjs/forrestjs/tree/master/packages/service-express
- Homepage: https://github.com/forrestjs/forrestjs/tree/master/packages/service-express#readme
- Issues: https://github.com/forrestjs/forrestjs/issues
- npm.io page: https://npm.io/package/@forrestjs/service-express

## Dependencies (3)

- [express](https://npm.io/package/express.md) ^4.17.1
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.11.2
- [@forrestjs/hooks](https://npm.io/package/@forrestjs/hooks.md) ^3.19.0

## Recent versions

- 3.19.0 (latest) — 2020-10-03
- 3.18.2 — 2020-09-30
- 3.18.1 — 2020-09-28
- 3.18.0 — 2020-09-28
- 3.17.0 — 2020-09-25
- 3.16.1 — 2020-09-13
- 3.16.0 — 2020-08-14
- 3.15.0 — 2020-08-13
- 3.14.0 — 2020-08-09
- 3.13.8 — 2020-08-09
- 3.13.7 — 2020-08-06
- 3.13.6 — 2020-08-06
- 3.13.5 — 2020-08-06
- 3.13.4 — 2020-08-06
- 3.13.3 — 2020-08-05
- … 101 more at https://npm.io/package/@forrestjs/service-express/versions

## README

# @forrestjs/service-express

ForrestJS service which helps setting up an ExpressJS server.

## Install & Setup

```bash
npm install --save @forrestjs/service-express
```

set it up in your ExpressJS App:

```js
// index.js
const { runHookApp } = require('@forrestjs/hooks')
const { EXPRESS_ROUTE, ...expressService } = require('@forrestjs/service-express')

// Create an Home Page for the Web App
const homePageRoute = (req, res) => res.send('Hello World')
const homePageAction = ({ registerRoute }) => registerRoute.get('/', homePageRoute)

// Run the app:
runHookApp({
    settings: {
        express: {
            port: 8080,
        },
    },
    services: [
        expressService,
    ],
    features: [
        [ EXPRESS_ROUTE, homePageAction ],
    ],
})
```

## Configuration & ENVs

### port

`express.port: 5000` sets up the port on which the server runs.

It falls back to environment variables:

- process.env.REACT_APP_PORT
- process.env.PORT
- 8080 (default value)

## Hooks

All the hooks exposed by `service-express` are _asynchronous_ and executes in _serie_.

### EXPRESS_HACKS_BEFORE

This hook fires before any other step.<br>
It receives a direct referenct to the `app` and the `server`.

### EXPRESS_MIDDLEWARE

This hook is good to inject custom App level middleware.

```js
const { EXPRESS_MIDDLEWARE } = require('@forrestjs/service-express')

registerAction({
    hook: EXPRESS_MIDDLEWARE,
    handler: ({ registerMiddleware }) =>
        registerMiddleware((req, res, next) => {
            req.data = 'foo'
            next()
        })
})
```

### EXPRESS_ROUTE

```js
const { EXPRESS_ROUTE } = require('@forrestjs/service-express')

registerAction({
    hook: EXPRESS_ROUTE,
    handler: ({ registerRoute }) =>
        registerRoute('get', '/', (req, res) => {
            res.send('Hello World')
        })
})
```

### EXPRESS_HANDLER

This hook is good to inject custom handlers or fallback routes that you
want to be sure fire after any other.

```js
const { EXPRESS_HANDLER } = require('@forrestjs/service-express')

registerAction({
    hook: EXPRESS_HANDLER,
    handler: ({ registerHandler }) =>
        registerHandler((err, req, res, next) => {
            req.send(err)
        })
})
```

### EXPRESS_HACKS_AFTER

This hook fires after any other step.<br>
It receives a direct referenct to the `app` and the `server`.

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