Node.js-服务器不是构造函数()ES6

Kay*_*Kay 1 node.js ecmascript-6

我正在创建一个节点服务器。我已经声明了一个服务器类,并且在构造函数中,我正在Express应用程序上调用listen。然后,我正在导出此Server类。

const instance = new Server(); ^ TypeError:服务器不是构造函数

index.js

const Server = require("./server");
const instance = new Server();
exports.default = instance.server;
Run Code Online (Sandbox Code Playgroud)

server.js

const App = require("./app");

class Server {

    constructor() {

        this.app = new App();
        this.instance = this.app.instance;
        this.config = this.app.config;
        this.server = this.instance.listen(this.config.port, "0.0.0.0");

        console.log("Server Running On: 0.0.0.0:" + this.config.port);

    }

}

exports.default = Server;
Run Code Online (Sandbox Code Playgroud)

网络包

const path = require("path");
const WebpackShellPlugin = require("webpack-shell-plugin");

module.exports = {

    mode: "development",
    entry: "./src/index.js",
    target: "node",
    devtool: "source-map",

    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "index.js",
        sourceMapFilename: "index.js.map"
    },
    module: {
        rules: [
            {
                enforce: "pre",
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "eslint-loader",
            },
            {
                test: /\.m?js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["@babel/preset-env"]
                    }
                }
            }
        ],
    },
    "plugins": [
        new WebpackShellPlugin({ onBuildEnd: ["nodemon dist/index.js"] }),
    ]

};
Run Code Online (Sandbox Code Playgroud)

npm

**    "serve": "webpack --watch",
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 5

您正在使用的require节点模块,而不是ES6模块

“默认”导出是完整的module.exports

更换

exports.default = Server;
Run Code Online (Sandbox Code Playgroud)

module.exports = Server;
Run Code Online (Sandbox Code Playgroud)