Skip to content

Commit

Permalink
docs: update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
simplymichael committed Jun 6, 2024
1 parent 8e3e6f1 commit a9af766
Showing 1 changed file with 57 additions and 25 deletions.
82 changes: 57 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# express-router-laravel
# node-laravel-router
A Laravel-inspired router for express.js.

(Forked from [Express Laravel Router](https://github.com/shaunpersad/express-laravel-router))
Expand All @@ -12,11 +12,13 @@ A Laravel-inspired router for express.js.
[![Coverage][codecov-image]][codecov-url]

## Motivation
This router is an alternative to the one that ships with express.js. Instead of manually creating instances of `express.Router`,
you can define your routes in group closures, where it becomes easier to create and reason about the shared properties of your routes.
This router is an alternative to routers such as the one that ships with express.js.
Instead of manually creating instances of `express.Router`, for example,
you can define your routes in group closures,
where it becomes easier to create and reason about the shared properties of your routes.

Also, this router allows you to execute custom code for each route definition, which can be useful for many things,
e.g. injecting dependencies into each request handler, or [automatically creating a swagger/openapi spec from your routes](https://github.com/simplymichael/express-router-laravel/wiki/Swagger-spec-generation-example).
e.g. injecting dependencies into each request handler, or [automatically creating a swagger/openapi spec from your routes](https://github.com/simplymichael/node-laravel-router/wiki/Swagger-spec-generation-example).

There are also some extra features like being able to name and generate urls strings for each route.

Expand All @@ -27,27 +29,26 @@ To summarize:

## Installation
```bash
npm install express-router-laravel --save
npm install node-laravel-router --save
```

## Quickstart

The below example will create two routes:
The examples below will create two routes:
1. A GET to `/api/v1/users/{userId}`
2. A POST to `/api/v1/auth`

### Express app example
```js
const express = require('express');
const createRouter = require('express-router-laravel').createRouter;
const createRouter = require('node-laravel-router').createRouter;

const app = express();
const router = createRouter(app);

router.group('/api', (router) => {

router.group('/v1', (router) => {

router.group('/users', (router) => {

router.get('/{userId}', (req, res) => { /* request handler logic */ });
});

Expand All @@ -74,8 +75,39 @@ apiRouter.use('/v1', v1Router);

app.use('/api', apiRouter);
```
The pure express.js version is not only visually harder to reason about, but it becomes increasingly more complex as more
routes and middleware are added.
The pure express.js version is not only visually harder to reason about,
but it becomes increasingly more complex as more routes and middleware are added.


### Generic Node app example
```js
const createRouter = require('node-laravel-router').createRouter;
const router = createRouter();

router.group('/api', (router) => {
router.group('/v1', (router) => {
router.group('/users', (router) => {
router.get('/{userId}', (req, res) => { /* request handler logic */ });
});

router.post('/auth', (req, res) => { /* request handler logic */ });
});
});

// When we pass an Express (or Express-type) app, like in the Express app example above,
// routing is automatically applied once we call the router's methods.
// With our generic, non-Express app, however, routing is not automatically.
// To apply the routing, we have to call the router's apply method as follows.
// Let's assume we are using an Express app:
router.apply((route) => {
// Do something with route.
// For example, assuming we have a connect-middleware-supported app
// stored in an `app` variable:
const { method, path, handlers } = route;

app[method](path, handlers);
});
```

## Usage
Our quickstart example used strings as the first argument for both the `router.group` and `router.get` methods. You are
Expand Down Expand Up @@ -106,7 +138,7 @@ router.group('/api', (router) => {

});
```
For more details, see the [wiki page](https://github.com/simplymichael/express-router-laravel/wiki/Group-options).
For more details, see the [wiki page](https://github.com/simplymichael/node-laravel-router/wiki/Group-options).

### Route options
The full route `options` object with their default values looks like this:
Expand Down Expand Up @@ -148,7 +180,7 @@ router.get('/api', (req, res) => {

});
```
For more details, see the [wiki page](https://github.com/simplymichael/express-router-laravel/wiki/Route-options).
For more details, see the [wiki page](https://github.com/simplymichael/node-laravel-router/wiki/Route-options).

## Full API
Below are all the methods available on a `router`.
Expand Down Expand Up @@ -268,17 +300,17 @@ Additionally, some of Laravel's naming has been updated or repurposed for clarit
in route definitions, and "namespace" in route groups.


[npm-url]: https://npmjs.com/package/express-router-laravel
[npm-version-image]: https://img.shields.io/npm/v/express-router-laravel
[npm-url]: https://npmjs.com/package/node-laravel-router
[npm-version-image]: https://img.shields.io/npm/v/node-laravel-router
[node-url]: https://nodejs.org/
[node-version-image]: https://img.shields.io/node/v/express-router-laravel
[package-url]: https://npm.im/express-router-laravel
[npm-downloads-image]: https://img.shields.io/npm/dm/express-router-laravel
[license-url]: https://github.com/simplymichael/express-router-laravel/blob/main/LICENSE.md
[license-image]: https://img.shields.io/github/license/simplymichael/express-router-laravel
[node-version-image]: https://img.shields.io/node/v/node-laravel-router
[package-url]: https://npm.im/node-laravel-router
[npm-downloads-image]: https://img.shields.io/npm/dm/node-laravel-router
[license-url]: https://github.com/simplymichael/node-laravel-router/blob/main/LICENSE.md
[license-image]: https://img.shields.io/github/license/simplymichael/node-laravel-router
[conventional-commits-url]: https://conventionalcommits.org
[conventional-commits-image]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-brightgreen.svg
[ci-url]: https://github.com/simplymichael/express-router-laravel/actions/workflows/run-coverage-tests.yml
[ci-image]: https://github.com/simplymichael/express-router-laravel/workflows/tests/badge.svg
[codecov-url]: https://codecov.io/gh/simplymichael/express-router-laravel
[codecov-image]: https://img.shields.io/codecov/c/github/simplymichael/express-router-laravel?token=9NNHXS8HDU
[ci-url]: https://github.com/simplymichael/node-laravel-router/actions/workflows/run-coverage-tests.yml
[ci-image]: https://github.com/simplymichael/node-laravel-router/workflows/tests/badge.svg
[codecov-url]: https://codecov.io/gh/simplymichael/node-laravel-router
[codecov-image]: https://img.shields.io/codecov/c/github/simplymichael/node-laravel-router?token=9NNHXS8HDU

0 comments on commit a9af766

Please sign in to comment.