koajs / trie-router Goto Github PK
View Code? Open in Web Editor NEWTrie-routing for Koa
License: MIT License
Trie-routing for Koa
License: MIT License
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.
this API is dumb
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)
Is there a reason why co-body isn't integrated with this module?
I wonder why "router.all" wasn't defined, althought "router.addRoute('ANY', paths, middleware)" can be easy to be used, I still expect you guys to implement "router.all".
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.
i hacked this up really quickly... a little non-trivial to read. if anyone's down to rewrite it, let me know!
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?
koa-compose needs a release
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)
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).
no method checking
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.
Is it possible to generate url for specific route not to hardcode them in templates?
Hi.
Trie-router is the official router and I think we could give second breath to it.
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]);
{String|{Array<String>}
{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
What do you think about it?
Is a catch all route possible?
the following doesn't work:
app.get(['/(.*)'], function* (next) {
...
app.post('/(.*)', function* (next) {
Thanks
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.
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.
๐ 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?
This doesn't work
require('trie-router')
This does
require('koa-trie-router')
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.