Mis*_*cki 1 javascript node.js express
我在下面写了代码,我有一个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 !')
});
}
}
Run Code Online (Sandbox Code Playgroud)
app.js 代码:
const Server = require('./Server');
const express = require('express');
const server = new Server();
server.start();
Run Code Online (Sandbox Code Playgroud)
您没有导出Server课程。在您的“Server.js”文件中,执行以下操作:
export default Server {
...
}
Run Code Online (Sandbox Code Playgroud)
并像这样离开你的“app.js”:
const Server = require("./Server");
Run Code Online (Sandbox Code Playgroud)
如果不使用 ES6,上述方法仅适用于 ES6 及更高版本:
class Server {
...
}
module.exports = Server;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1697 次 |
| 最近记录: |