# alamanah-express

> A backend framework using express.

Latest version **2.3.2** (published 2021-04-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install alamanah-express
pnpm add alamanah-express
yarn add alamanah-express
bun add alamanah-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 | 2.3.2 |
| Published | 2021-04-29 |
| First published | 2021-04-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 32.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Saief El Gebali |
| Maintainers | saiefelgebali |

## Links

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

## Dependencies (1)

- [express](https://npm.io/package/express.md) ^4.17.1

## Recent versions

- 2.3.2 (latest) — 2021-04-29
- 2.2.2 — 2021-04-07
- 2.2.1 — 2021-04-07
- 2.2.0 — 2021-04-07
- 2.1.0 — 2021-04-07
- 2.0.1 — 2021-04-07
- 2.0.0 — 2021-04-07
- 1.0.7 — 2021-04-06
- 1.0.6 — 2021-04-06
- 1.0.5 — 2021-04-06
- 1.0.4 — 2021-04-05
- 1.0.3 — 2021-04-05
- 1.0.2 — 2021-04-05
- 1.0.1 — 2021-04-04

## README

# Al Amanah

Taking inspiration from Django, Al Amanah uses express to implement a basic routing system & add some more functionality.
- *Al Amanah comes in especially useful when needing to quickly initialize a REST API.*

## Setup
- Run `npm install alamanah-express` to install the npm package
- Use the [`alamanah-cli`](https://github.com/saiefelgebali/alamanah-cli) to quickly start a new project *(Optional)*

## Starting a Server
All you need to start a server, is a `port` and an array of `routes`.
```javascript
import alamanah, { route } from "alamanah-express";

const routes = [
    route({ path: '/hello', method: "GET", view: (req, res) => res.send("Hello world!") }),
    route({ path: '/goodbye', method: "GET", view: (req, res) => res.send("Goodbye world!") }),
]

const server = alamanah({
    port: 5000,
    routes
});
```

## Routing
Import the `route` function from the alamanah-express module to use the routing system.

The `route` function takes in one of two objects, an `EndRoute` object or a `BaseRoute` object.

`EndRoute` objects are your standard URL endpoints, whereas `BaseRoute` objects are routers to those endpoints.

### Creating an EndRoute:
To create an EndRoute, pass in the `path`, `method` and `view` parameters.
```javascript
const route = route({ path: '/hello', method: "GET", view: (req, res) => res.send("Hello world!") });
```

### Creating a BaseRoute:
To create a BaseRoute, pass in the `path` and `routes` parameters.
```javascript
const routes = [route1, route2, route3];
const base = route({ path: '/hello', routes: routes });
```

### Mixing EndRoutes and BaseRoutes
```javascript
import alamanah, { route } from "alamanah-express";

const helloRoutes = [
    route({ path: '/world', method: "GET", view: (req, res) => res.send("Hello world!") }),
    route({ path: '/people', method: "GET", view: (req, res) => res.send("Hello people!") }),
]

const routes = [
    route({ path: '/hello', routes: helloRoutes }),
    route({ path: '/goodbye', method: "GET", view: (req, res) => res.send("Goodbye world!") }),
]

const server = alamanah({
    port: 5000,
    routes
});
```
The `/hello` route in this example, is now a `BaseRoute`, since it takes in an array of routes.

The `/goodbye` route is still an `EndRoute`.

Now our server has the following URL endpoints,
- `GET /hello/world`
- `GET /hello/people`
- `GET /goodbye`

## Applying Middleware
You can apply express middleware by putting them into an array and passing them to the server.
```javascript
...
const logger = (req, res, next) => {
    console.log(req.ip);
    next();
}

const middleware = [
    logger
]

const server = alamanah({
    port: 5000,
    routes,
    middleware
});
```

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