-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo5.dart~
51 lines (40 loc) · 1.13 KB
/
demo5.dart~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import 'dart:io';
import 'dart:async';
void main(){
HttpServer.bind('127.0.0.1', 8080).then((dynamic server) => serverHandler(server));
print('server created');
}
void serverHandler(dynamic server){
server.listen((HttpRequest request) {
Future getPath = getFromPath(request.uri.path);
getPath.then((String responseText){
request.response.write('$responseText');
request.response.close();
})
.catchError((e){
request.response.write('$e');
request.response.close();
});
});
}
Future<String> getFromPath(var path){
var completer = new Completer();
String stringPath = path.toString() == '/' ? '/index.html' : path.toString();
String pathString;
pathString =stringPath.substring(1, stringPath.length);
if(pathString.startsWith('demo'))
completer.complete('error 500');
else{
File file = new File('$pathString');
String responseTe;
file.readAsString().then((String text){
responseTe = text;
completer.complete(responseTe);
})
.catchError((e){
responseTe = 'error 404 ';
completer.complete(responseTe);
});
}
return completer.future;
}