Node.js - сервер не является конструктором () ES6 - PullRequest
0 голосов
/ 09 октября 2018

Я создаю сервер узла.Я объявил серверный класс и в конструкторе я вызываю listen в приложении Express.Затем я экспортирую этот класс сервера.

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 голосов
/ 09 октября 2018

Вы используете require, то есть узловые модули , а не ES6-модули .

Экспорт по умолчанию завершен module.exports.

Заменить

exports.default = Server;

на

module.exports = Server;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...