Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import 'dart:async';
import 'dart:io';
void main() {
TestHttpServer().createHttpServer();
}
class TestHttpServer {
HttpServer server;
Future<void> createHttpServer() async {
server = await HttpServer.bind(
InternetAddress.loopbackIPv4,
3000,
);
print('Listening on localhost:${server.port}');
await for (HttpRequest request in server) {
request.response.headers.add('Content-Type', 'text/html');
//sets up the default route
if (request.uri.path == "/") {
request.response
..write('''
<center>
<h1>Home sweet home</h1>
</center>
''')
..close();
}
if (request.uri.path == "/nash") {
request.response
..write('''
<center>
<h1>Someone actually cares</h1>
</center>
''')
..close();
print("Some one is looking me up?");
}
if (request.uri.path == "/john") {
request.response
..write('''
<center>
<h1 style="color:red;"> Why hello John.... </h1>
</center>
''')
..close();
print("This is what I was rambling on about");
}
if (request.uri.path == "/secret") {
request.response
..write('''
<center>
<h1 style="color:red;"> This is a top secret route.... </h1>
</center>
''')
..close();
print("Someone is accessing top secret");
}
}
}
void killServer() {
server.close(force: true);
print('Server is now dead');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment