expressjs-router
Advanced tools
Comparing version 1.1.2 to 1.1.3
{ | ||
"name": "expressjs-router", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Structured routes for ExpressJS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,3 +0,3 @@ | ||
Express Router | ||
============== | ||
Express.js Router [](https://travis-ci.org/olivierkaisin/express-router) | ||
================= | ||
@@ -7,20 +7,41 @@ Structured routing for **Express.JS** | ||
### Example `route.js` | ||
## Example | ||
Project structure: | ||
``` | ||
. | ||
+-- routes | ||
| +-- getStatus.js | ||
+-- models | ||
| +-- MyModel.js | ||
+-- app.js | ||
``` | ||
### Route definition `getStatus.js` | ||
```javascript | ||
// File: ./routes/getStatus.js | ||
"use strict"; | ||
var MyModel = require("./models/MyModel"); | ||
var MyModel = require("../models/MyModel"); | ||
function validate(req, res, next) { | ||
// The synchronous validate function is executed before the responder, allowing | ||
// to easily check for the existence of parameters and their format. | ||
// | ||
// If a parameter doesn't follow the specification, the router will automatically | ||
// respond with an HTTP 400 status and output validation errors in a JSON. | ||
function validate(req, res) { | ||
req.assert("id") | ||
.notEmpty().isInt(); | ||
next(req, res, next); | ||
} | ||
// The respond method is the actual implementation of our route | ||
function respond(req, res, next) { | ||
@@ -41,2 +62,7 @@ var id = req.param("id"); | ||
// The exports describe the route definition. | ||
// .path, .method and .respond are required | ||
// | ||
// Conditions are checked to define wether the route should be | ||
// executed or not. You can define as many conditions as you want. | ||
module.exports = { | ||
@@ -46,11 +72,17 @@ path: "/status/:id", | ||
validate : validate, | ||
respond : respond | ||
validate : validate, | ||
respond : respond, | ||
conditions : ["loginRequired"] | ||
}; | ||
``` | ||
### Express `app.js` | ||
### How to use with your express app | ||
```javascript | ||
// File: ./app.js | ||
```javascript | ||
"use strict"; | ||
var express = require("express"); | ||
@@ -62,2 +94,12 @@ var expressRouter = require("expressjs-router"); | ||
// By enabling debug mode, routes will be logged to stdout | ||
expressRouter.enableDebug(); | ||
// This conditional will allow us to require login on routes | ||
expressRouter.createConditional("loginRequired", function (req, res) { | ||
return !!req.user; | ||
}); | ||
// We finally create our routes by giving the path where they are stored | ||
expressRouter.create(app, "./path/to/routes"); | ||
@@ -74,1 +116,6 @@ | ||
2. You can enable debug mode by calling `expressRouter.enableDebug();` | ||
### License: | ||
MIT |
9841
117