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 new function to handle querystring in url #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.0.2 /2018-11-14
=================

* map query string parameters from original url to rewrite url

3.0.1 / 2018-05-08
==================
Expand Down
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));
app.use(rewrite('/js/(.*)', '/public/assets/js/$1'));
```

```js
app.use(rewrite('/home?foo=bar', '/new-home?foo=$1'));
```

## Debugging

Use the __DEBUG__ environment variable with "koa-rewrite":
Expand Down
3 changes: 3 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));
// GET /js/jquery.js
app.use(rewrite('/js/(.*)', '/public/assets/js/$1'));

//Get /home?foo=bar
app.use(rewrite('/home/:foo', '/new-home?foo=$1'));

app.use(function(ctx) {
ctx.body = ctx.url + '\n';
});
Expand Down
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

const debug = require('debug')('koa-rewrite');
const toRegexp = require('path-to-regexp');
const parse = require('url-parse');
const queryString = require('query-string');

/**
* Expose `expose`.
Expand All @@ -30,7 +32,8 @@ function rewrite(src, dst) {

return function(ctx, next) {
const orig = ctx.url;
const m = re.exec(orig);
const pathUrl = handleQueryString(orig);
const m = re.exec(pathUrl);

if (m) {
ctx.url = dst.replace(/\$(\d+)|(?::(\w+))/g, (_, n, name) => {
Expand Down Expand Up @@ -67,3 +70,21 @@ function toMap(params) {

return map;
}

/**
* convert query string url to path string.
*
* @param {String} src
* @return {String}
* @api private
*/

function handleQueryString(src) {
let url = parse(src, false);
if(url.query){
let queryparams = queryString.parse(url.query);
return `${url.pathname}/${Object.values(queryparams).join('/')}`;
}else{
return src;
}
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "koa-rewrite",
"description": "URL rewrite middleware for koa",
"repository": "koajs/rewrite",
"version": "3.0.1",
"version": "3.0.2",
"scripts": {
"test": "mocha test/test"
},
Expand All @@ -24,7 +24,9 @@
"license": "MIT",
"dependencies": {
"debug": "^3.1.0",
"path-to-regexp": "^2.1.0"
"path-to-regexp": "^2.1.0",
"query-string": "^6.2.0",
"url-parse": "1.4.4"
},
"engines": {
"node": ">= 4"
Expand Down
28 changes: 27 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('new Koa-rewrite', () => {
it('rewrite /?foo=bar -> /home?foo=bar', done => {
const app = new Koa();
app.use(differentPathHelper);
app.use(rewrite(/^\/((\?)(.*?))?$/, '/home$2$3'));
app.use(rewrite('/(.*)/:foo', '/home?foo=$2'));
app.use((ctx) => {
ctx.body = ctx.url;
});
Expand All @@ -115,4 +115,30 @@ describe('new Koa-rewrite', () => {
.expect('/home', done);
});

it('rewrite /home?foo=bar -> /new-home?foo=:foo', done => {
const app = new Koa();
app.use(differentPathHelper);
app.use(rewrite('/home/:foo', '/new-home?foo=$1'));
app.use((ctx) => {
ctx.body = ctx.url;
});

request(app.callback())
.get('/home?foo=bar')
.expect('/new-home?foo=bar', done);
});

it('rewrite /home?foo=bar&test=bar2 -> /new-home?foo=:foo&test=:test', done => {
const app = new Koa();
app.use(differentPathHelper);
app.use(rewrite('/home/:foo/:test', '/new-home?foo=$1&test=$2'));
app.use((ctx) => {
ctx.body = ctx.url;
});

request(app.callback())
.get('/home?foo=bar&test=bar2')
.expect('/new-home?foo=bar&test=bar2', done);
});

});