Skip to content

Commit

Permalink
Added support for other http methods than just get
Browse files Browse the repository at this point in the history
  • Loading branch information
mallocator committed Sep 7, 2016
1 parent da4f788 commit 688e43a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
18 changes: 11 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var semver = require('semver');

/**
* @typedef {Object} RouterMapping
* @property {string[]} paths All the paths that have been mapped to this router so far
* @property {Router} instance The router instance that handles the actual requests
* @property {Object<string, string[]>} paths All the paths that have been mapped to this router so far for each method
* @property {Router} instance The router instance that handles the actual requests
*/

/**
Expand Down Expand Up @@ -71,7 +71,7 @@ function Router(configuration = {}) {
if (endpoint.toString().startsWith('/v:'+ configuration.param)) {
throw new Error('Versioned paths will be generated automatically, please avoid prefixing paths');
}
let methodRouter = getRouter(endpoint);
let methodRouter = getRouter(endpoint, method);
if (typeof version == 'function') {
handlers.unshift(version);
version = [];
Expand Down Expand Up @@ -103,20 +103,24 @@ function Router(configuration = {}) {
* router assigned already and returns that one. If all routers are already using the given route, a new router is
* returned.
* @param {string|RegExp} endpoint The endpoint for which we want a router
* @param {string} method The http method we want to use
* @property {RouterMapping[]} routers The list of existing routers
* @property {RouterConfig} configuration The router configuration for all generated routers.
* @returns {Router}
*/
function generateRouter(endpoint) {
function generateRouter(endpoint, method) {
for (let router of this.routers) {
if (router.paths.indexOf(endpoint) == -1) {
router.paths.push(endpoint);
if (!router.paths[method]) {
router.paths[method] = [];
}
if (router.paths[method].indexOf(endpoint) == -1) {
router.paths[method].push(endpoint);
return router.instance;
}
}
let router = express.Router(this.configuration);
this.routers.push({
paths: [ endpoint ],
paths: { [method]: [ endpoint ] },
instance: router
});
return router;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-version-router",
"version": "0.5.0",
"version": "0.5.1",
"description": "A router for express that manages api versioning.",
"license": "Apache-2.0",
"author": "Ravi Gairola <mallox@pyxzl.net>",
Expand Down
12 changes: 12 additions & 0 deletions test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,16 @@ describe('Router', () => {
app.use(router);
request(app).get('/test?v=1').expect(200, 'success').end(done);
});

it('should be able to support other request methods on the same path', done => {
var router = Router();
router.get('/test', 1, (req, res, next) => next(), (req, res) => res.end('success get'));
router.post('/test', 1, (req, res, next) => next(), (req, res) => res.end('success post'));

var app = express();
app.use(router);
request(app).get('/test?v=1').expect(200, 'success get').end(() => {
request(app).post('/test?v=1').expect(200, 'success post').end(done);
});
});
});

0 comments on commit 688e43a

Please sign in to comment.