Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for query params in rewrite targets #373

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/server/rewriter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var express = require('express')
var url = require('url')
var _ = require('lodash')

module.exports = function (routes) {
var router = express.Router()
Expand All @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion test/server/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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) {
Expand Down