gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:45:09 25 4
gpt4 key购买 nike

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

const instance = new Server(); ^ TypeError: Server is not a constructor

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",

最佳答案

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

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

替换

exports.default = Server;

module.exports = Server;

关于Node.js - 服务器不是构造函数() ES6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52717535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com