Dart Shelf server different static files on different routes

83 views Asked by At

Is it possible to serve different static files on different routes? This is what I've tried but it does not work:

import 'dart:io';

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf_static/shelf_static.dart';

void main(List<String> args) async {
  final ip = InternetAddress.anyIPv4;

  var staticHandler1 = Pipeline().addHandler(
      createStaticHandler('../one/web', defaultDocument: 'index.html'));

  var staticHandler2 = Pipeline().addHandler(
      createStaticHandler('../two/web', defaultDocument: 'index.html'));

  final router = Router()
    ..get('/one', staticHandler1)
    ..get('/two', staticHandler2);

  // For running in containers, we respect the PORT environment variable.
  final port = int.parse(Platform.environment['PORT'] ?? '8080');
  final server = await serve(router, ip, port);
  print('Server listening on port ${server.port}');
}
0

There are 0 answers