我正在尝试让一个控制台应用程序作为 Web 服务器启动,所以如果我在浏览器中浏览到 locahost:3000,我应该会看到一个包含 HELLO WORLD 的空白页面!写在上面,但我得到的只是控制台面板中的重复消息“AcceptEx failed: 10022”:
import 'dart:io';
void main() {
HttpServer.bind(InternetAddress.ANY_IP_V4, 3000).then((server){
server.listen((HttpRequest request) {
request.response.write('''
<html>
<head>
</head>
<body>
<div>
HELLO WORLD!
</div>
</body>
</html>
''');
});
});
}
更新:
我将它设置为 ANY_IP_V6,这是错误的,我的系统没有那个,将它设置为 ANY_IP_V4 似乎确实有助于解决这个问题,但现在它最终到达了一个提示空引用异常的地步 异常中断:空对象没有“取消”方法。
来自类 Object 中的 noSuchMethod。
最佳答案
尝试将 InternetAddress
替换为 localhost,并确保关闭响应。像这样:
HttpServer.bind('127.0.0.1', 3000).then((server){
server.listen((HttpRequest request) {
request.response.write('''
<html>
<head>
</head>
<body>
<div>
HELLO WORLD!
</div>
</body>
</html>
''');
request.response.close();
});
});
关于webserver - Dart,一个 super 简单的http服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20275872/