类的构造函数不能没有新的调用
我是新的面向对象编程和更多的NodeJS特殊,我已经尽力重定向路由到其它类,但我总是发现这个问题时,试图把这个路线
类型错误:类构造函数路由器不能没有援引“新”
我试过很多不同的方式,但它没有工作
//this is the fuction exists into the app.js
const index = require('./serverSide/router/index');
initRoutes() {
app.use('/admin', index);
app.use('/', (req, res) => {
res.sendFile(__dirname + './src/index.html');
});
app.use(function (req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
}
//and this is into the class Router
class Router {
constructor(router) {
console.log('/**Routing**/');
router.get('/', this.result());
}
result(req, res) {
console.log("works");
}
}
module.exports = Router;
回答如下:
你需要调用new
一个类时,你包括constructor
。欲了解更多信息点击这里:https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Classes