Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending express.Router

Is there a way to extend express.Router ?

I tried this :

class Test extends express.Router() {

};

But express throws me an error.

Any solution ?

like image 227
auk Avatar asked Feb 06 '16 14:02

auk


People also ask

What does Express router () do?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.

What does next () do in Express?

The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.

What is Express routing?

A route is a section of Express code that associates an HTTP verb ( GET , POST , PUT , DELETE , etc.), a URL path/pattern, and a function that is called to handle that pattern.

How do you create routes in an Express application?

Create a new file called things. js and type the following in it. var express = require('express'); var router = express. Router(); router.


1 Answers

The right way to do it:

class Test extends express.Router {
    constructor() {
        super();
        this.get('/', (req, res) => console.log('test'));
    }
};

When you write express.Router() (with parentheses) you already call the constructor and therefore you're trying to extend an object instead of class.

like image 111
Kreozot Avatar answered Sep 21 '22 03:09

Kreozot