Express: How can I subclass the Router?

Created on 29 Sep 2015  路  5Comments  路  Source: expressjs/express

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

4.x question

Most helpful comment

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!

All 5 comments

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

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!

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

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Sunriselegacy picture Sunriselegacy  路  3Comments

cuni0716 picture cuni0716  路  3Comments

wxs77577 picture wxs77577  路  3Comments

zackarychapple picture zackarychapple  路  3Comments

dmaks9 picture dmaks9  路  3Comments