-
Notifications
You must be signed in to change notification settings - Fork 14.9k
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
How can I subclass the Router? #2768
Comments
Hi! The Express 4.x router cannot be inherited; you have to use the |
Here is a simple example, using what you provided above: var express = require('express')
var request = require('supertest')
var Router = require('router')
var util = require('util')
function MyRouter() {
return Router.call(this, arguments)
}
util.inherits(MyRouter, Router)
MyRouter.prototype.get = function(){
console.log('do some fancy shenanigans')
MyRouter.super_.prototype.get.apply(this, arguments)
}
var router = new MyRouter()
var app = express()
app.use(router)
router.get('/', function (req, res) {
res.send('hello, world!')
})
request(app)
.get('/')
.expect(200, 'hello, world!', function (err) {
if (err) throw err
console.log('success')
})
This is a great suggestion! Since Express 4.x does not allow you to create a custom router, I'm not sure where such documentation could ever live in the Express docs. Perhaps one day when 5.0 is released and there are Express 5.0 docs, it could probably live there :) Documentation requests can be filed at https://github.com/strongloop/expressjs.com for our docs team! |
Thanks for the fast reply! |
It's no problem!
Put what there? The link seems to go to Express 4.x source code, but it's impossible to extend the Express 4.x router. We did fix that in Express 5, if you're willing to play with the alpha :) |
Sorry for the confusion. In Express 5, I would imagine no docs are necessary unless it isn't straight forward. |
…not work - Express v4's Router cannot be subclassed (see expressjs/express#2768).
Hello,
I am effectively trying to do the following:
This however is not working and I believe is linked to #2413.
I apologize if this is the wrong place for this question, but, I pose it here since the following was stated in the thread:
More over, I couldn't help but feel like creating a custom
Router
should be documented.Note: I have tried many, MANY combinations of inheritance but the above was the most simple/standard
The text was updated successfully, but these errors were encountered: