Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (36 sloc) 1.15 KB
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Serves the index.html in the same directory as this script.
// Use the URL localhost:4044 in your browser.
// For a similar server that uses http_server package
// see basic_file_server.dart.
import 'dart:async';
import 'dart:io';
File targetFile = File('index.html');
Future main() async {
var server;
try {
server = await HttpServer.bind(InternetAddress.loopbackIPv4, 4044);
} catch (e) {
print("Couldn't bind to port 4044: $e");
await for (HttpRequest req in server) {
if (await targetFile.exists()) {
print("Serving ${targetFile.path}.");
req.response.headers.contentType = ContentType.html;
try {
await targetFile.openRead().pipe(req.response);
} catch (e) {
print("Couldn't read file: $e");
} else {
print("Can't open ${targetFile.path}.");
..statusCode = HttpStatus.notFound
You can’t perform that action at this time.