我在下面编写了代码,但出现了 TypeError: Server is not a constructor
并且我不明白为什么以及如何修复它。
Server.js 代码:
const express = require('express');
class Server {
constructor() {
this.app = express();
this.app.get('/', function(req, res) {
res.send('Hello World');
})
}
start() {
this.app.listen(8080, function() {
console.log('MPS application is listening on port 8080 !')
});
}
}
app.js 代码:
const Server = require('./Server');
const express = require('express');
const server = new Server();
server.start();
最佳答案
您没有导出 Server
类。在您的“Server.js”文件中,执行此操作:
export default Server {
...
}
然后像这样留下你的“app.js”:
const Server = require("./Server");
上述方法只适用于 ES6 及以上版本,如果不使用 ES6:
class Server {
...
}
module.exports = Server;
关于javascript - 类型错误 : is not a constructor with Node and Express JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872652/