2.3.2 • Published 3 years ago

alamanah-express v2.3.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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

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.

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.

const routes = [route1, route2, route3];
const base = route({ path: '/hello', routes: routes });

Mixing EndRoutes and BaseRoutes

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.

...
const logger = (req, res, next) => {
    console.log(req.ip);
    next();
}

const middleware = [
    logger
]

const server = alamanah({
    port: 5000,
    routes,
    middleware
});
2.3.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.2.2

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.2

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago