Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果没有新的_Javascript_Node.js_Oop_Module - Fatal编程技术网

Javascript 如果没有新的

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

我是面向对象编程的新手,对nodejs更为特殊,我曾尝试将路由重定向到其他类,但在尝试调用此路由时总是发现这个问题

TypeError:在没有“new”的情况下无法调用类构造函数路由器

我试过很多不同的方法,但都不管用

//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类?是的,确实如此