我在下面编写了代码,我有一个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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句