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

How can I subclass the Router? #2768

Closed
reallistic opened this issue Sep 29, 2015 · 5 comments
Closed

How can I subclass the Router? #2768

reallistic opened this issue Sep 29, 2015 · 5 comments
Assignees

Comments

@reallistic
Copy link

Hello,
I am effectively trying to do the following:

var Router = require('express').Router;
var util = require('util');

function MyRouter(){
   return Router.apply(this, arguments);
}
util.inherits(MyRouter, Router);
MyRouter.prototype.get = function(){
   //do some fancy shenanigans
   MyRouter.super_.get.apply(this, arguments);
}

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:

If you are not happy you cannot extend Express's router's prototype, then either use the router module

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

@dougwilson
Copy link
Contributor

Hi! The Express 4.x router cannot be inherited; you have to use the router module OR use Express 5.0.0-alpha.2

@dougwilson
Copy link
Contributor

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')
})

More over, I couldn't help but feel like creating a custom Router should be documented.

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!

@dougwilson dougwilson self-assigned this Sep 29, 2015
@reallistic
Copy link
Author

Thanks for the fast reply!
I would actually put it here right beside the "magic".

@dougwilson
Copy link
Contributor

It's no problem!

I would actually put it here right beside the "magic".

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 :)

@reallistic
Copy link
Author

Sorry for the confusion.
I meant I would put a disclaimer at that line stating this form of object/class construction doesn't allow for inheritance.

In Express 5, I would imagine no docs are necessary unless it isn't straight forward.

shdnx added a commit to shdnx/next-express that referenced this issue Apr 6, 2018
…not work - Express v4's Router cannot be subclassed (see expressjs/express#2768).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants