[英]Node.js: Requiring a class does not work
嗨,我正在嘗試使用es6類創建一個node.js服務器。 由於某些原因,要求我的服務器類不起作用,並且我不知道為什么它不起作用。
PS:要求配置按預期工作。
節點版本: 5.7.0
Server.js:
"use strict"
const express = require('express')
const path = require('path')
class Server {
constructor(configuration) {
this.title = "Points Dashboard";
this.app = express();
this.configureAuthentication(configuration)
}
listen(port, callback) {
this.app.listen(port, callback);
}
}
module.exports = Server;
app.js:
"use strict"
const configuration = require('./lib/configuration');
const Server = require('./lib/Server')
const server = new Server(configuration)
server.listen(configuration.server.public_port, () => {
console.log(`Server running on ${configuration.server.public_url}, internal port ${configuration.server.public_port}`)
})
錯誤:
TypeError:服務器不是函數
...調用棧
在對象。 (/root/app.js:6:16)
項目結構:
調試日志:
最終我發現了錯誤,這確實是一個愚蠢的錯誤。 我上面發布的代碼是正確的。 我在這個問題中沒有提到的是,我正在使用docker。 在寫我的docker文件時,我忘記添加一行來將本地計算機上的代碼與容器內運行的代碼同步。 在創建容器時,我忘記添加module.exports = Server;
這就是為什么require無效的原因。
我正在使用Visual Studio代碼在Docker容器中調試代碼,但是由於我從未更改app.js中的內容,因此無法看到容器中運行的代碼與本地代碼不同。
謝謝你的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.