Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"Dart2Native File Server Tutorial" solution code for tutorial on YouTube
import 'dart:io';
import 'package:args/args.dart';
import 'package:http_server/http_server.dart';
main(List<String> arguments) async {
ArgParser parser = ArgParser()
..addOption('dir', defaultsTo: 'web')
..addOption('port', defaultsTo: '8085');
var parsedResults = parser.parse(arguments);
var serveDir = parsedResults['dir'];
var port = int.tryParse(parsedResults['port']);
HttpServer server = await HttpServer.bind('localhost', port);
VirtualDirectory virDir = VirtualDirectory(serveDir)
..allowDirectoryListing = true;
virDir.directoryHandler = (dir, request) {
var indexUri = Uri.file(dir.path).resolve('index.html');
virDir.serveFile(File(indexUri.toFilePath()), request);
};
await for (HttpRequest request in server) {
await virDir.serveRequest(request);
}
}
name: static_file_server
description: A sample command-line application.
version: 1.0.0
homepage: https://www.creativebracket.com
author: Jermaine Oppong
environment:
sdk: '>=2.5.0 <3.0.0'
dependencies:
args: ^1.5.2
http_server: ^0.9.8+3
path: ^1.6.0
dev_dependencies:
pedantic: ^1.8.0
test: ^1.6.0
@graphicbeacon
Copy link
Author

graphicbeacon commented Dec 31, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment