-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.dart
30 lines (26 loc) · 916 Bytes
/
server.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
import 'dart:io';
void main() {
// Specify the path to the "web" directory
var pathToServe = 'web';
// Bind the server to localhost on port 8080
HttpServer.bind(InternetAddress.loopbackIPv4, 8080).then((server) {
print('Server running on localhost:${server.port}');
// Listen for requests
server.listen((HttpRequest request) {
// Construct the file path from the request URI
var file = File('$pathToServe${Platform.pathSeparator}${request.uri.path}');
// Check if the file exists
file.exists().then((bool exists) {
if (exists) {
// Serve the file
file.openRead().pipe(request.response);
} else {
// File not found, return a 404 response
request.response.statusCode = HttpStatus.notFound;
request.response.write('File not found');
request.response.close();
}
});
});
});
}