Created
March 12, 2016 04:10
-
-
Save Caaz/be2b4453e701f7859c71 to your computer and use it in GitHub Desktop.
Test server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:io"; | |
void main() { | |
startServer(InternetAddress.ANY_IP_V4,3333); | |
} | |
void startServer(host, int port) { | |
HttpServer | |
// HttpServer object | |
.bind(host,port) | |
// bind returns a Future<HttpServer> object. Future objects have the method then, which is called when it's child type is ready. | |
.then((HttpServer server) { | |
server.listen(handleRequest); | |
print("Listening at ${server.address.host}:${server.port}"); | |
}); | |
} | |
void handleRequest(HttpRequest req) { | |
print("Hey a request at ${req.uri}"); | |
File file = new File(".${req.uri}"); | |
req.response | |
..statusCode = HttpStatus.OK | |
..headers.contentType = ContentType.HTML | |
..addStream( | |
file.openRead().asBroadcastStream() | |
) | |
.then( | |
(HttpResponse res) => res.close() | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment