我在 Dart 中不断收到错误“HTTP 标头不可变”。
我想要做的是当用户在“包”文件夹中请求某些东西时,它会获取它。
我的错误代码是这样的:
if(new RegExp("/packages/(.*)").hasMatch(request.uri.toString())){
new File(request.uri.toString().substring(1)).readAsString().then((String contents){
request.response.headers.contentType = new ContentType("text", "css", charset: "utf-8");
request.response.write(contents);
request.response.close();
});
}
我的整个代码是这样的:
import "dart:io";
void main() {
HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8000).then((HttpServer server){
print("Listening on localhost on {$server.port}");
server.listen((HttpRequest request){
switch(request.uri.toString()){
case "/":
request.response.headers.contentType = ContentType.HTML;
new File("static/taskbar.html").readAsString().then((String contents){
request.response.write(contents);
request.response.close();
new File("static/index.html").readAsString().then((String contents){
request.response.write(contents);
request.response.close();
});
});
break;
default:
request.response.statusCode = HttpStatus.NOT_FOUND;
request.response.write("404 Not Found.");
request.response.close();
break;
}
if(new RegExp("/packages/(.*)").hasMatch(request.uri.toString())){
new File(request.uri.toString().substring(1)).readAsString().then((String contents){
request.response.headers.contentType = new ContentType("text", "css", charset: "utf-8");
request.response.write(contents);
request.response.close();
});
}
});
});
}