Skip to content

Instantly share code, notes, and snippets.

@eyelash
Created May 18, 2019 09:26
Show Gist options
  • Save eyelash/b90ce161872957b8dfcdead1b0768110 to your computer and use it in GitHub Desktop.
Save eyelash/b90ce161872957b8dfcdead1b0768110 to your computer and use it in GitHub Desktop.
import 'dart:io';
String elem(String tag, String text) {
return '<$tag>$text</$tag>';
}
String h1(String text) {
return elem('h1', text);
}
String p(String text) {
return elem('p', text);
}
Future runServer() async {
var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 4040);
await for (HttpRequest request in server) {
request.response.headers.contentType = ContentType.html;
request.response.write(h1('Hello, world!'));
request.response.write(p(request.uri.path));
request.response.close();
}
}
runServerSync() {
HttpServer.bind(InternetAddress.loopbackIPv4, 4040).then((server) {
server.listen((request) {
request.response.headers.contentType = ContentType.html;
request.response.write(h1('Hello, world!'));
request.response.write(p(request.uri.path));
request.response.close();
});
});
}
Future main(List<String> arguments) async {
await runServer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment