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

时间:2018-10-09 09:22:37

标签: node.js ecmascript-6

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

  

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

index.js

const Server = require("./server");
const instance = new Server();
exports.default = instance.server;

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;

Webpack

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"] }),
    ]

};

npm

**    "serve": "webpack --watch",

1 个答案:

答案 0 :(得分:5)

您正在使用require,即node modules,而不是ES6 modules

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

替换

exports.default = Server;

使用

module.exports = Server;