Javascript 如果没有新的
我是面向对象编程的新手,对nodejs更为特殊,我曾尝试将路由重定向到其他类,但在尝试调用此路由时总是发现这个问题 TypeError:在没有“new”的情况下无法调用类构造函数路由器 我试过很多不同的方法,但都不管用Javascript 如果没有新的,javascript,node.js,oop,module,Javascript,Node.js,Oop,Module,我是面向对象编程的新手,对nodejs更为特殊,我曾尝试将路由重定向到其他类,但在尝试调用此路由时总是发现这个问题 TypeError:在没有“new”的情况下无法调用类构造函数路由器 我试过很多不同的方法,但都不管用 //this is the fuction exists into the app.js const index = require('./serverSide/router/index'); initRoutes() { app.use('/admin', ind
//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
。有关更多信息,请参见:app.js调用Router类?是的,确实如此