diff --git a/.gitignore b/.gitignore index 51409dc..61f2900 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ *~ *.log node_modules -*.env diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index e49c834..57a0a1f 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -2,13 +2,49 @@ You can see below the API reference of this module. -### Plugin Configuration +### `bloggify:init(config)` +If the routes (default: `app/routes`) folder exists, it will dictate the routes structure. For example: +`_` is alias for `index`. `_foo` is alias for `:foo` + +``` +routes/ +├── 404.ajs << 404 page +├── _.ajs << The index page (loaded on `/`) +├── api << Rest API (`/api`) +│   ├── index.js << Api Handlers (e.g. sending a custom 404) +│   └── users << The `/api/users[/...]` endpoint +│   ├── index.js << Handling `/api/users` (sending the list of users) +│   └── _username << Handling `/api/users/:username` +│   └── index.js << Fetching the user, by username, and sending it +└── users << The users list, in HTML format (`/users`) + ├── _.ajs << The `/users` view + ├── _.js << The `/users` controler + └── _user << `/users/:user` endpoint + ├── _.ajs << View + └── _.js << Controller +``` + +The controller files look like this: + +```js +// Before hook +exports.before = (ctx, cb) => ... + +// After hook +exports.after = (ctx, cb) => ... + +// Handle all the methods +// Alias for module.exports = ctx => ... +exports.all = ctx => ... + +// Handle post requests +exports.post = ctx => ... +``` + +#### Params - **Object** `config`: - `routes_dir` (String): The path to the directory where the routes are stored. They should be randable view files. For dynamic routes, use the `_`character. - - `controllers_dir` (String): The path to the controllers directory. - - `error_pages` (Object): The error pages template names. - - `404` (String): The path to the `404` error page. - - `500` (String): The path to the `500` error page. - - `bad_csrf` (String): The path the template that should be rendered when a bad CSRF token is sent to the server. + +"/path/to/route": { controllerPath, viewPath } diff --git a/README.md b/README.md index e815368..70516fb 100644 --- a/README.md +++ b/README.md @@ -12,87 +12,54 @@ $ npm i --save bloggify-flexible-router ``` -## :clipboard: Example - +## :memo: Documentation -```js -// Config -/* -{ - "bloggify-flexible-router": { - "controllers_dir": "app/controllers", - "routes_dir": "app/routes", - "error_pages": { - "404": "404.ajs", - "500": "500.ajs", - "bad_csrf": "422.ajs" - } - } -} -*/ - -// The app/routes contains: -// ├── 404.ajs -// ├── 422.ajs -// ├── 500.ajs -// └── users -// └── _user -// └── index.ajs - -// The app/controllers contain the controllers. For instance, app/users/_user/index.js would look like this: -const USERS = { - "Alice": { - name: "Alice" - , location: "Earth" - } -}; - -// Before middleware -exports.before = (ctx, cb) => { /* ... */ }; - -// GET requests -// You can use `all`, `post`, `get` etc. -exports.get = (ctx, cb) => { - const user = USERS[ctx.params.user]; - - // End with a 404 if the user is not found - if (!user) { - return ctx.next(); - } - - // Pass the data to the template - cb(null, { - user - }); -}; - -// After middleware -exports.after = (ctx, cb) => { /* ... */ }; -``` +### `bloggify:init(config)` +If the routes (default: `app/routes`) folder exists, it will dictate the routes structure. For example: -## :question: Get Help +`_` is alias for `index`. `_foo` is alias for `:foo` -There are few ways to get help: +``` +routes/ +├── 404.ajs << 404 page +├── _.ajs << The index page (loaded on `/`) +├── api << Rest API (`/api`) +│   ├── index.js << Api Handlers (e.g. sending a custom 404) +│   └── users << The `/api/users[/...]` endpoint +│   ├── index.js << Handling `/api/users` (sending the list of users) +│   └── _username << Handling `/api/users/:username` +│   └── index.js << Fetching the user, by username, and sending it +└── users << The users list, in HTML format (`/users`) + ├── _.ajs << The `/users` view + ├── _.js << The `/users` controler + └── _user << `/users/:user` endpoint + ├── _.ajs << View + └── _.js << Controller +``` - 1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question. - 2. For bug reports and feature requests, open issues. :bug: - 3. For direct and quick help from me, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket: +The controller files look like this: +```js +// Before hook +exports.before = (ctx, cb) => ... -## :memo: Documentation +// After hook +exports.after = (ctx, cb) => ... +// Handle all the methods +// Alias for module.exports = ctx => ... +exports.all = ctx => ... -### `bloggify:init(config)` +// Handle post requests +exports.post = ctx => ... +``` #### Params - **Object** `config`: - `routes_dir` (String): The path to the directory where the routes are stored. They should be randable view files. For dynamic routes, use the `_`character. - - `controllers_dir` (String): The path to the controllers directory. - - `error_pages` (Object): The error pages template names. - - `404` (String): The path to the `404` error page. - - `500` (String): The path to the `500` error page. - - `bad_csrf` (String): The path the template that should be rendered when a bad CSRF token is sent to the server. + +"/path/to/route": { controllerPath, viewPath }