Giter Club home page Giter Club logo

rewrite's Introduction

URL rewrite middleware for koa.

Notice: koa-rewrite@2+ supports koa@2, if you want to use this module with koa@1, please use koa-rewrite@1.

Installation

$ npm install koa-rewrite

Examples

Rewrite using a regular expression, rewriting /i123 to /items/123.

app.use(rewrite(/^\/i(\w+)/, '/items/$1'));

Rewrite using route parameters, references may be named or numeric. For example rewrite /foo..bar to /commits/foo/to/bar:

app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));

You may also use the wildcard * to soak up several segments, for example /js/vendor/jquery.js would become /public/assets/js/vendor/jquery.js:

app.use(rewrite('/js/(.*)', '/public/assets/js/$1'));

Debugging

Use the DEBUG environment variable with "koa-rewrite":

koa-rewrite rewrite /i123 -> /items/123 +762ms
koa-rewrite rewrite /i321 -> /items/321 +9s
koa-rewrite rewrite /i123 -> /items/123 +5s

License

MIT

rewrite's People

Contributors

3imed-jaberi avatar 75lb avatar jinphen avatar jonathanong avatar juliangruber avatar macalinao avatar nswbmw avatar tejasmanohar avatar tj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rewrite's Issues

support rewrite for qs

Is it acceptable to support rewrite from qs?

Such as

/items?id=1

to

/items/1

If so, I'll submit a PR.

Query parameters are missing after a rewrite

I am using koa-rewrite ver 3.0.0. I specified the following in my app:

app.use(rewrite(/^\/p\/didit/, '/p/customers/section_id/didits'));

After the rewrite is executed, all of the query parameters are gone. I traced it down to the following line in index.js of koa-rewrite module:

ctx.url = dst.replace(/\$(\d+)|(?::(\w+))/g, (_, n, name) => {
        if (name) return m[map[name].index + 1];
        return m[n];
      });

I am not sure why setting the url would cause the query parameters to be gone.

add a flag

would be useful to have a flag to indicate that a path has been rewritten. maybe ctx.rewrite = true or similar. would you accept a PR?

map query string to rewrite url

Hi Guys,

Is it possible to map query string from original url to rewrite url, something like this.

/experience/testing-main?ddate='10-10-10' 
to
/some-random-url?ddate=$1

Please advise if its possible.

Thanks

app.use() requires a generator function

I am using Koa@1 and koa-rewrite@1 and try to implement functionality like this.

rewritelogic.js

const rewrite = require('koa-rewrite');
function rewriteLogic(){
  rewrite('/english/experience/dev1', '/english/experience/dev');
}

module.exports = {
  rewriteLogic,
}

And then use this file in my app.js

app.use(rewrite.rewriteLogic)

But I am facing this error

AssertionError [ERR_ASSERTION]: app.use() requires a generator function

Can you help me on this ?

I removed this error by providing generator function but then rewrite functionality did not worked.

const rewrite = require('koa-rewrite');
function* rewriteLogic(next){
  rewrite('/english/experience/dev1', '/english/experience/dev');
  yield next;
}
module.exports = {
  rewriteLogic,
}

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.