Skip to content

Commit

Permalink
Docs: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
aichbauer committed May 8, 2017
1 parent 76abb12 commit 8e3c49c
Showing 1 changed file with 65 additions and 74 deletions.
139 changes: 65 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,80 @@

[![Build Status](https://travis-ci.org/rudolfsonjunior/express-routes-mapper.svg?branch=master)](https://travis-ci.org/rudolfsonjunior/express-routes-mapper) [![Coverage Status](https://coveralls.io/repos/github/rudolfsonjunior/express-routes-mapper/badge.svg)](https://coveralls.io/github/rudolfsonjunior/express-routes-mapper)

> a simple package to map your routes for your expressjs application
> A simple package to map your routes for your expressjs application
## getting started
## Getting started

- [start from ground](#start-from-ground)
- [supported methods](#supported-methods)
- [dynamic routes](#dynamic-routes)
- [set path to controller](#set-path-to-controller)
- [Install](#install)
- [Use](#use)
- [Routes](#routes)
- [Controller](#controller)
- [Express with mapped Routes](#express-with-mapped-routes)
- [Supported Methods](#supported-methods)
- [Dynamic Routes](#dynamic-routes)

## start from ground
## Install

This is a example for a simple rest API.
```sh
$ npm i -S express-routes-mapper
```

### 1.) npm install
or

```sh
$ npm i -S express-routes-mapper
$ yarn add express-routes-mapper
```

### 2.) mapped routes
## Use

Create your routes file:
After the installation you can import the package to your express project.

### Routes

Create your routes file:

```js
const routes = {
'POST /user': 'UserController.create'
};

export default routes;
export default routes; // module.exports = routes;
```

Every post request to your server to route '/user' will call the function 'create' on the 'UserController'.

### 3.) the controller
### Controller

Create a file named UserController.js

```js
// es6 class syntax
export default class UserController {
create (req, res) {
res.send('created a User with es6');
res.send('created a User with es6 class syntax');
};
};

// object factory pattern
const UserController = () => {
const create = (req, res) => {
res.send('created a User with es6 without a class syntax');
res.send('created a User with without es6 class syntax');
};

return {
create,
};
};

export default UserController;
export default UserController; // module.exports = UserController;
```


### 4.) tell express.js app to use our routes

I assume you have a folder structure like this, but it can be adapted to any folder structure.
### Express with mapped Routes

If you have a different, folder structure, and want to link to a different path look [here](#set-path-to-controller).
I assume you have a folder structure like this, but it can be adapted to any folder structure.

```
```sh
.
+-- src
| +-- config
Expand All @@ -88,22 +94,26 @@ package.json
Your app.js could look a bit like this:

The magic happens here:
* `import routes from './config/routes';` the file where all the routes are mapped
* `import mapRoutes from 'express-routes-mapper';` the package that makes the mapping possible
* `const mappedRoutes = mapRoutes(routes);` tell router to use your routes
* `app.use('/', mappedRoutes);` tell express to use the mapped routes

- `import routes from './config/routes';` the file where all the routes are mapped
- `import mapRoutes from 'express-routes-mapper';` the package that makes the mapping possible
- `const mappedRoutes = mapRoutes(routes, 'src/controllers/');` tell router to use your routes
- `app.use('/', mappedRoutes);` tell express to use the mapped routes

```js
import express from 'express';
import http from 'http';
import express from 'express'; // const express = require('express');
import http from 'http'; // const http = require('http');

import mapRoutes from 'express-routes-mapper';
import routes from './config/routes';
import mapRoutes from 'express-routes-mapper'; // const mapRoutes = require('express-routes-mapper');
import routes from './config/routes'; // const routes = require('./config/routes');

const app = express();
const server = http.Server(app);
const port = 4444;
const mappedRoutes = mapRoutes(routes);
// mapRoutes takes two arguments
// - 1. the routes
// - 2. the path to your controllers from process.cwd();
const mappedRoutes = mapRoutes(routes, 'src/controllers/');

app.use('/', mappedRoutes);

Expand All @@ -115,64 +125,45 @@ server.listen(port, () => {

## Supported methods

* GET
* POST
* PUT
* DELETE
- **GET**
- **POST**
- **PUT**
- **DELETE**

```js
{
'GET /someroute' : 'SomeController.somefunction',
'POST /someroute' : 'SomeController.somefunction',
'PUT /someroute' : 'SomeController.somefunction',
'DELETE /someroute' : 'SomeController.somefunction'
}
const routes = {
'GET /someroute' : 'SomeController.somefunction',
'POST /someroute' : 'SomeController.somefunction',
'PUT /someroute' : 'SomeController.somefunction',
'DELETE /someroute' : 'SomeController.somefunction',
};
```

## Dynamic routes
## Dynamic Routes

Simply use a colon ':' for defining dynamic routes.
Simply use a colon `:` for defining dynamic routes.

```js
{
'GET /someroute/:id' : 'SomeController.somefunction'
}
const routes = {
'GET /someroute/:id' : 'SomeController.someFunction',
};
```

If you make a get request to `http://localhost/someroute/1` the 1 (:id) is now in the 'SomeController accessible.
If you make a get request to `http://localhost/someroute/1` the number `1` (:id) is now in the `SomeController` accessible.

```js
export default class SomeController {
somefunction (req, res) {
// object factory pattern
const SomeController = () => {
const someFunction = (req, res) => {
const id = req.params.id;

// do some fency stuff with the id
};
};
```

## set path to controller

The only differnce is that you pass in the path to your file in the mapRoutes function.
* `const mappedRoutes = mapRoutes(routes, 'path/to/new/file/')` this path is relative to your root directoy (directory with the package.json)
* `app.use('/', mappedRoutes);`

```js
import express from 'express';
import http from 'http';

import mapRoutes from 'express-routes-mapper';
import routes from './config/routes';

const app = express();
const server = http.Server(app);
const port = 4444;
const mappedRoutes = mapRoutes(routes, 'path/to/new/file/');

app.use('/', mappedRoutes);
return {
someFunction,
};
};

server.listen(port, () => {
console.log('There we go ♕');
console.log(`Gladly listening on http://127.0.0.1:${port}`);
});
export default SomeController; // module.exports = SomeController;
```

0 comments on commit 8e3c49c

Please sign in to comment.