diff --git a/src/server/rewriter.js b/src/server/rewriter.js index e61cc4b95..3549c32a2 100644 --- a/src/server/rewriter.js +++ b/src/server/rewriter.js @@ -1,4 +1,6 @@ var express = require('express') +var url = require('url') +var _ = require('lodash') module.exports = function (routes) { var router = express.Router() @@ -12,6 +14,10 @@ module.exports = function (routes) { target = target.replace(':' + param, req.params[param]) } req.url = target + if (target.indexOf('?')) { + // create query from target + _.assign(req.query, url.parse(target, true).query) + } next() }) } else { diff --git a/test/server/plural.js b/test/server/plural.js index cf55b24c0..dbbcf39f4 100644 --- a/test/server/plural.js +++ b/test/server/plural.js @@ -74,7 +74,8 @@ describe('Server', function () { server.use(jsonServer.defaults()) server.use(jsonServer.rewriter({ '/api/': '/', - '/blog/posts/:id/show': '/posts/:id' + '/blog/posts/:id/show': '/posts/:id', + '/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body' })) server.use(router) }) @@ -647,6 +648,13 @@ describe('Server', function () { .expect(db.posts[0]) .end(done) }) + + it('should rewrite using params and query', function (done) { + request(server) + .get('/comments/special/1-quux') + .expect([db.comments[4]]) + .end(done) + }) }) describe('router.render', function (done) {