Coder Social home page Coder Social logo

trie-router's People

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  avatar  avatar

trie-router's Issues

fail fast when user passes non-generator function

The api allows me to pass a non-generator function, but at runtime, an error is thrown:

it('works', function(done){
  app.get('/go', function() { // <==========
    this.status = 204
  })

  request(server)
  .get('/go')
  .expect(204, done)
})
TypeError: Cannot read property 'next' of undefined
      at Object.trieRouter (/Users/aheckmann/test/trie-router/lib/dispatcher.js:33:16)
      at GeneratorFunctionPrototype.next (native)
      at Object.respond (/Users/aheckmann/test/trie-router/node_modules/koa/lib/application.js:176:10)
      at GeneratorFunctionPrototype.next (native)
      at Object.<anonymous> (/Users/aheckmann/test/trie-router/node_modules/koa-compose/index.js:29:12)
      at GeneratorFunctionPrototype.next (native)
      at next (/Users/aheckmann/test/trie-router/node_modules/koa/node_modules/co/index.js:74:21)
      at Object.<anonymous> (/Users/aheckmann/test/trie-router/node_modules/koa/node_modules/co/index.js:45:5)
      at Server.<anonymous> (/Users/aheckmann/test/trie-router/node_modules/koa/lib/application.js:123:8)
      at Server.EventEmitter.emit (events.js:110:17)
      at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:504:12)
      at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
      at Socket.socketOnData (_http_server.js:357:22)
      at Socket.EventEmitter.emit (events.js:107:17)
      at readableAddChunk (_stream_readable.js:159:16)
      at Socket.Readable.push (_stream_readable.js:126:10)
      at TCP.onread (net.js:514:20)

I think the api should either throw when a user passes a non-generator function or changes to support non-generator functions.

Weird error at console

var koa = require('koa');
var app = koa();

require('koa-trie-router')(app)

app.use(app.router)
app.route('/')
.get(function* (next) {
  this.body = 'homepage'
});

app.listen(3000);

generates the following console error

C:\Repository\Notable>nodemon --harmony server.js
22 Dec 21:49:18 - [nodemon] v0.7.10
22 Dec 21:49:18 - [nodemon] to restart at any time, enter `rs`
22 Dec 21:49:18 - [nodemon] watching: C:\Repository\Notable
22 Dec 21:49:18 - [nodemon] starting `node --harmony server.js`
TypeError: Object function noop(done){
  done();
} has no method 'next'
    at Object.<anonymous> (C:\Repository\Notable\node_modules\koa-trie-router\lib\dispatcher.js:11:21)
    at GeneratorFunctionPrototype.next (native)
    at next (C:\Repository\Notable\node_modules\koa\node_modules\co\index.js:55:19)
    at Object.<anonymous> (C:\Repository\Notable\node_modules\koa\node_modules\co\index.js:91:5)
    at next (C:\Repository\Notable\node_modules\koa\node_modules\co\index.js:74:19)
    at Object.<anonymous> (C:\Repository\Notable\node_modules\koa\node_modules\co\index.js:91:5)
    at next (C:\Repository\Notable\node_modules\koa\node_modules\co\index.js:74:19)
    at C:\Repository\Notable\node_modules\koa\node_modules\co\index.js:91:5
    at Server.<anonymous> (C:\Repository\Notable\node_modules\koa\lib\application.js:102:22)
    at Server.EventEmitter.emit (events.js:101:17)

Params support in `use`

I am not sure if that is already supported and I just don't see it. I want to "dynamically" mount a koa app with routes to a variable route like this:

const appRouter = () => {
  return new Router()
  .use(ctx => {
    // has ctx.params.app
    // has ctx.params.id if its specified in the route,
  })
  .get("/:id", ctx => {
    // has ctx.params.app
    // has ctx.params.id,
  })
  .middleware()

app.use(mount("/:app", appRouter()))

Which should enable routes like:

/myapp/1
/myapp/2

Even better if I could design the approuter to be totally unaware of the /:app part of the route and only would have to deal with its own routes. Then I would like to move the processing of potential id params to a middleware that is called before the approuter and sets some infos in the ctx before passing control to the approuter.

Separate routes into files?

How do I separate routes into files?

app.js:

const Koa = require('koa')
    const get = require('./routes/get')
    const post = require('./routes/post')
    
    const app = new Koa()
    
    app.use(get)
    app.use(post)
    app.listen(3000)

routes/get.js:

'use strict'
    
    const Router = require('koa-trie-router')
    
    const router = new Router()
    
    // middleware that is specific to this router
    router.use(async (ctx, next) => {
      console.log('Time: ', Date.now())
      await next()
    })
    
    // define the home page route
    router.get('/', async (ctx, next) => {
      ctx.type = 'json'
      ctx.body = {
        message: 'Birds home page'
      }
    })
    
    // Separate this post route in a new file.
    // router.post('/', async (ctx, next) => {
    //   ctx.type = 'json'
    //   ctx.body = {
    //     message: 'Post birds home page'
    //   }
    // })
    
    module.exports = router.middleware()

routes/post.js:

 'use strict'
    
    const Router = require('koa-trie-router')
    
    const router = new Router()
    
    router.post('/', async (ctx, next) => {
      ctx.type = 'json'
      ctx.body = {
        message: 'Post birds home page'
      }
    })
    
    module.exports = router.middleware()

When I try to post it on my Postman at http://127.0.1.1:3000/:

Method Not Allowed

Any ideas how can I get around to this?

1.0.0

koa-compose needs a release

Error if used without any middleware

I just got this error when I was testing this router. When using middleware that respons with something it seems to work fine.

TypeError: Object function noop(done){
done();
} has no method 'next'
at Object. (/Users/thomas/CloudStation/Dropbox/www/koala/node_modules/koa-trie-router/lib/dispatcher.js:11:21)
at GeneratorFunctionPrototype.next (native)
at next (/Users/thomas/CloudStation/Dropbox/www/koala/node_modules/koa/node_modules/co/index.js:55:19)
at Object. (/Users/thomas/CloudStation/Dropbox/www/koala/node_modules/koa/node_modules/co/index.js:91:5)
at next (/Users/thomas/CloudStation/Dropbox/www/koala/node_modules/koa/node_modules/co/index.js:74:19)
at Object. (/Users/thomas/CloudStation/Dropbox/www/koala/node_modules/koa/node_modules/co/index.js:91:5)
at next (/Users/thomas/CloudStation/Dropbox/www/koala/node_modules/koa/node_modules/co/index.js:74:19)
at /Users/thomas/CloudStation/Dropbox/www/koala/node_modules/koa/node_modules/co/index.js:91:5
at Server. (/Users/thomas/CloudStation/Dropbox/www/koala/node_modules/koa/lib/application.js:102:22)
at Server.EventEmitter.emit (events.js:101:17)

Should not throw error for malformed URL

routington throws error if it fails to parse URL with decodeURIComponent().

This could cause some problem when using w/ trie-router + Koa.

For example, we could create a simple server and then request for URL /%. Then this error occurs:

Possibly Unhandled Rejection at: Promise
Promise { <rejected> [NotFoundError: malformed url: /%] } 
reason:  [NotFoundError: malformed url: /%]

Since it throws an error explicitly, we could not gather this kind of error w/ Koa's app.on("error") or middleware.

I think rather than making changes for routington, we could consider catching the assertion error in trie-router. Because trie-router is restricted to a narrower use case (for Koa only).

.all

no method checking

No `next` passed inside routes

var koa = require('koa');
var app = koa();

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

app.route('/').get(function* (next) {
  console.log(next);
  this.status = 200;
});

app.listen(3333);
console.log('koa started on port 3333');

This output undefined. I'm pretty sure this is not the intended behavior.

Do you have any plans for supporting koa@2 and make using more convenient?

Hi.

Trie-router is the official router and I think we could give second breath to it.

Proposal \ my vision

Requirements

  1. It should works with node 7.x.x and higher
  2. It should support only koa@2 and higher
  3. It should be mountable
  4. It should be powerful as it is now

API

  • Instantiation should requires new operator (like Koa@2)

    let router = new Router();
  • Routers could be define like

    router[use|get|post|put|del|patch]([path], [...middleware]);
    • where path & middleware both optional but it requires at least one of them
    • path is {String|{Array<String>}
    • middleware is {Function|AsyncFunction|Array<[Function|AsyncFunction]>}
  • Router should implements router.middleware() like koa.middleware() for mounting (composing)

  • ctx.params like https://github.com/koajs/trie-router/tree/1.1.0#thisparams

  • https://github.com/koajs/trie-router/tree/1.1.0#features

What do you think about it?

catch all route?

Is a catch all route possible?

the following doesn't work:

app.get(['/(.*)'], function* (next) {
...
app.post('/(.*)', function* (next) {

Thanks

1.0.9 breaks REST-style Routes

This change broke all of my routes that have child routes. For example if my setup is:

app.get( '/api/v1', _getAllRecords);  // will have 1 child elements
app.get( '/api/v1/table1', _getTableRecords);  // will have 1 child elements
app.get( '/api/v1/table1/row1', _getRowRecord); // will have 0 child elements

The only call that will work is the last one because of line 39 of dispatcher.js:

if (Object.keys(node.child).length) return true;

What was the logic for this change? How should I setup my routes? I thought they were quite straightforward REST API routes but I guess not.

405 -> 404

A request made to a route that has not been defined is returning a 405 instead of 404.

it('missing route should 404', function(done) {
  app.get('/trailing-slash/', function*() {
    this.status = 204
  })

  request(server)
  .get('/trailing-slash')
  .expect(404, done)  // Error: expected 404 "Not Found", got 405 "Method Not Allowed"
});

If the case was that /trailing-slash was defined but not for a GET then a 405 would make sense.

fails to route OPTIONS

๐Ÿ‘‹ I have an issue to report, looks like a bug to me but i find it weird noone else has figured this out. I might as well be doing something wrong.

Here goes, ...

any router.options(...) route registered middleware is never ran, instead all OPTIONS requests seem to be handled by a fixed code.

This is an issue for when a specific cors preflight middleware needs to be mounted to a routes OPTIONS

wdyt?

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.