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

supports custom routing detect path: ctx.routerPath #73

Merged
merged 1 commit into from
Nov 14, 2014

Conversation

fengmk2
Copy link
Contributor

@fengmk2 fengmk2 commented Aug 22, 2014

We want to bind helloworld.example.com/users => example.com/helloworld/users

@fengmk2
Copy link
Contributor Author

fengmk2 commented Aug 23, 2014

@alexmingoia Any reviews?

@alexmingoia
Copy link
Collaborator

I understand the use case, but I don't think it necessitates extending koa-router. The same thing can be accomplished with middleware:

var koa = require('koa');
var Router = require('koa-router');

var app = koa();
var router = new Router();

// responds to fengmk2.example.com
router.get('/:user', function *(next) {
  console.log(this.params.user);
  // => "fengmk2"
  console.log(this.path);
  // => "/fengmk2"
  console.log(this.originalPath);
  // => "/"
});

// all routes for this router have paths rewritten to include subdomains
app.use(rewriteSubdomain, router.middleware());

// rewrites ctx.path to include subdomain
function rewriteSubdomain *(next) {
  var subdomain = this.headers.host.replace('example.com', '').split('.').join('');
  if (subdomain) {
    this.originalPath = this.path;
    this.path = '/' + subdomain + (this.path === '/' ? '' : this.path);
  }
  yield next;
};

@fengmk2
Copy link
Contributor Author

fengmk2 commented Aug 24, 2014

I first do this in my project, but it change the path, that confuse for the
path is not the real current request url...

@alexmingoia
Copy link
Collaborator

I misunderstood. You can also respond to subdomains without changing the request path:

var koa = require('koa');
var Router = require('koa-router');

var app = koa();
var router = new Router();

// responds to fengmk2.example.com/users
router.get('/users', function *(next) {
  console.log(this.subdomain); // => "fengmk2";
  console.log(this.path);  // => "/users"
  this.body = (this.subdomain + this.path);
});

app
  .use(function *subdomainMiddleware (next) {
    this.subdomain = this.header.host.replace('example.com', '').split('.').join('');
    yield next;
  })
  .use(router.middleware());

@fengmk2
Copy link
Contributor Author

fengmk2 commented Aug 24, 2014

I show my codes to explain:

var VHOST_RE = /^([a-z]\w*)\.[\w\-]+\.[\w\-]+/i;
app.use(function* vhost(next) {
    var hostname = this.hostname;
    var m = VHOST_RE.exec(hostname);
    if (m) {
      var appname = m[1];
      if (ignoreNames.indexOf(appname) === -1) {
        var prefixPath = '/' + appname;
        if (this.path.indexOf(prefixPath) !== 0) {
          this.routerPath = prefixPath + this.path;
        }
      }
    }
    yield* next;
 });

app.use(require('koa-router')(app));

// vhost 
// localhost:7001/demo/ <=> demo.test.com:7001/ <=> demo.test.com:7001/demo/
app.get('/demo/', app.controllers.demo.home);
app.post('/demo/update', app.controllers.demo.update);
$ curl demo.test.com:7001/
$ curl demo.test.com:7001/demo/

# hope both requests get the same response body: `hello demo`

@alexmingoia alexmingoia merged commit a6ce0a1 into ZijianHe:master Nov 14, 2014
@fengmk2
Copy link
Contributor Author

fengmk2 commented Nov 14, 2014

wow, awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants