Is there a way to extend express.Router
?
I tried this :
class Test extends express.Router() {
};
But express throws me an error.
Any solution ?
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.
The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.
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.
Create a new file called things. js and type the following in it. var express = require('express'); var router = express. Router(); router.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With