Skip to content

Commit

Permalink
update req.query when rewrite without params typicode#431
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed ayoub committed Dec 7, 2016
1 parent 7c402a8 commit 712f134
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/server/rewriter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const express = require('express')
const url = require('url')
const _ = require('lodash')

function updateQueryString(target,sourceUrl) {
return !!~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {};
}
module.exports = (routes) => {
const router = express.Router()

Expand All @@ -14,16 +16,14 @@ module.exports = (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)
}
req.query = updateQueryString(req.query,req.url)
next()
})
} else {
router.all(route + '*', (req, res, next) => {
// Rewrite url by replacing prefix
req.url = req.url.replace(route, routes[route])
req.query = updateQueryString(req.query,req.url)
next()
})
}
Expand Down

0 comments on commit 712f134

Please sign in to comment.