├── .travis.yml ├── .editorconfig ├── test ├── mock.js ├── no-callback.js ├── sync.js └── index.js ├── .gitignore ├── package.json ├── index.js ├── LICENSE └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | install: 5 | - npm install 6 | script: 7 | - npm test 8 | after_script: NODE_ENV=test istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage 9 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | trim_trailing_whitespace = true 8 | 9 | [*.js] 10 | 11 | indent_style = space 12 | indent_size = 4 13 | 14 | [{package.json,.travis.yml}] 15 | indent_style = space 16 | indent_size = 2 17 | 18 | [*.md] 19 | trim_trailing_whitespace = false 20 | -------------------------------------------------------------------------------- /test/mock.js: -------------------------------------------------------------------------------- 1 | 2 | // @routePrefix('/api') 3 | module.exports = { 4 | 5 | //@route('/collection') 6 | getAll: function(req, res){ 7 | res.send('OK'); 8 | }, 9 | 10 | // @route('/entity/:id') 11 | getOne: function(req, res){ 12 | res.send('OK'); 13 | }, 14 | 15 | // @httpPost() 16 | // @route('/test') 17 | test: function(req, res){ 18 | res.sendStatus(201); 19 | }, 20 | }; 21 | -------------------------------------------------------------------------------- /test/no-callback.js: -------------------------------------------------------------------------------- 1 | var should = require('should'); 2 | var express = require('express'); 3 | 4 | var expressRouter = require('../index.js'); 5 | 6 | var mockPath = 'mock.js'; 7 | 8 | 9 | describe('when no callback is given', function(){ 10 | 11 | var app; 12 | 13 | before(function(){ 14 | app = express(); 15 | }); 16 | 17 | it('should not throw an error', function(done){ 18 | expressRouter(app, mockPath); 19 | 20 | setTimeout(done, 1000); 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-annotation", 3 | "version": "0.3.0", 4 | "description": "Bring annotation routing to express", 5 | "keywords": [ 6 | "express", 7 | "annotation", 8 | "decorator", 9 | "router" 10 | ], 11 | "main": "index.js", 12 | "scripts": { 13 | "test": "mocha" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/mastilver/express-annotation.git" 18 | }, 19 | "author": "Thomas Sileghem", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/mastilver/express-annotation/issues" 23 | }, 24 | "homepage": "https://github.com/mastilver/express-annotation", 25 | "dependencies": { 26 | "annotation-router": "^0.8.0", 27 | "to-absolute-path": "^1.0.0" 28 | }, 29 | "devDependencies": { 30 | "coveralls": "^2.11.2", 31 | "express": "^4.12.3", 32 | "istanbul": "^0.3.13", 33 | "mocha": "^2.2.4", 34 | "should": "^5.2.0", 35 | "supertest": "^0.15.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var annotationRouter = require('annotation-router'); 2 | var toAbsolutePath = require('to-absolute-path'); 3 | 4 | 5 | module.exports = function(app, paths, callback){ 6 | 7 | paths = toAbsolutePath(paths, 1); 8 | 9 | var registerRoute = registerApp(app); 10 | 11 | annotationRouter(paths, function(err, route){ 12 | 13 | if(err) return callCallback(err); 14 | 15 | registerRoute(route); 16 | }, 17 | function(err){ 18 | callCallback(err); 19 | }); 20 | 21 | function callCallback(err){ 22 | if(callback != null){ 23 | callback(err); 24 | } 25 | } 26 | }; 27 | 28 | module.exports.sync = function(app, paths){ 29 | 30 | paths = toAbsolutePath(paths, 1); 31 | 32 | var registerRoute = registerApp(app); 33 | 34 | annotationRouter 35 | .sync(paths) 36 | .forEach(registerRoute); 37 | }; 38 | 39 | function registerApp(app){ 40 | return function(route){ 41 | app[route.method.toLowerCase()](route.url, route.action); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test/sync.js: -------------------------------------------------------------------------------- 1 | var request = require('supertest'); 2 | var should = require('should'); 3 | var express = require('express'); 4 | 5 | var expressRouter = require('../index.js'); 6 | 7 | var mockPath = 'mock.js'; 8 | 9 | describe('register routes', function(){ 10 | 11 | var app; 12 | 13 | before(function(){ 14 | 15 | app = express(); 16 | 17 | expressRouter.sync(app, mockPath); 18 | }); 19 | 20 | it('should have register: GET /api/collection', function(done){ 21 | request(app) 22 | .get('/api/collection') 23 | .expect(200) 24 | .expect('OK', done); 25 | }); 26 | 27 | it('should have register: GET /api/entity/:id', function(done){ 28 | request(app) 29 | .get('/api/entity/1') 30 | .expect(200) 31 | .expect('OK', done); 32 | }); 33 | 34 | it('should have register: POST /api/test', function(done){ 35 | request(app) 36 | .post('/api/test') 37 | .expect(201) 38 | .expect('Created', done); 39 | }); 40 | 41 | }); 42 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var request = require('supertest'); 2 | var should = require('should'); 3 | var express = require('express'); 4 | 5 | var expressRouter = require('../index.js'); 6 | 7 | var mockPath = 'mock.js'; 8 | 9 | describe('register routes', function(){ 10 | 11 | var app; 12 | 13 | before(function(done){ 14 | 15 | app = express(); 16 | 17 | expressRouter(app, mockPath, done); 18 | }); 19 | 20 | it('should have register: GET /api/collection', function(done){ 21 | request(app) 22 | .get('/api/collection') 23 | .expect(200) 24 | .expect('OK', done); 25 | }); 26 | 27 | it('should have register: GET /api/entity/:id', function(done){ 28 | request(app) 29 | .get('/api/entity/1') 30 | .expect(200) 31 | .expect('OK', done); 32 | }); 33 | 34 | it('should have register: POST /api/test', function(done){ 35 | request(app) 36 | .post('/api/test') 37 | .expect(201) 38 | .expect('Created', done); 39 | }); 40 | 41 | }); 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Sileghem Thomas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # express-annotation [![Build Status](https://travis-ci.org/mastilver/express-annotation.svg?branch=master)](https://travis-ci.org/mastilver/express-annotation) [![Coverage Status](https://coveralls.io/repos/mastilver/express-annotation/badge.svg?branch=master)](https://coveralls.io/r/mastilver/express-annotation?branch=master) 2 | 3 | 4 | > Bring annotation routing to [expressJs](https://github.com/strongloop/express) 5 | 6 | 7 | ## Install 8 | 9 | `$ npm install --save express-annotation` 10 | 11 | ## Usage 12 | 13 | given a file `/controllers/user.js`: 14 | 15 | ``` 16 | 17 | // @route('/users') 18 | module.exports.getAll = function(req, res){ 19 | 20 | }; 21 | 22 | // @route('/users/:id') 23 | module.exports.get = function(req, res){ 24 | console.log(req.params.id); 25 | }; 26 | 27 | // @httpPut() 28 | // @route('/users/:id') 29 | module.exports.update = function(req, res){ 30 | 31 | }; 32 | 33 | ``` 34 | 35 | you can register those routes by writing so: 36 | 37 | ``` 38 | 39 | var expressAnnotation = require('express-annotation'); 40 | var app = require('express')(); 41 | 42 | expressAnnotation(app, 'controllers/**/*.js', function(err){ 43 | 44 | // routes have been added to the express instance 45 | 46 | app.listen(3030); 47 | }); 48 | 49 | ``` 50 | 51 | ## API 52 | 53 | ### expressAnnotation(expressInstance, paths, callback) 54 | 55 | #### expressInstance 56 | 57 | *required* 58 | > express instance (value returned by: `express()`) 59 | 60 | #### paths 61 | 62 | *required* 63 | Type: `array`, `string` 64 | 65 | #### callback(err) 66 | 67 | Type: `Function` 68 | > will be called when all the routes have been added to the express instance 69 | 70 | 71 | ### [Annotations / Decorators API](https://github.com/mastilver/node-annotation-router#annotations-api) 72 | 73 | 74 | ## Licence 75 | 76 | MIT © Thomas Sileghem 77 | --------------------------------------------------------------------------------