Skip to content

Instantly share code, notes, and snippets.

@Caaz
Created March 12, 2016 04:10
Show Gist options
  • Save Caaz/be2b4453e701f7859c71 to your computer and use it in GitHub Desktop.
Save Caaz/be2b4453e701f7859c71 to your computer and use it in GitHub Desktop.
Test server
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