dart-archive / shelf_static

archived repo
https://github.com/dart-lang/shelf/tree/master/pkgs/shelf_static
BSD 3-Clause "New" or "Revised" License
24 stars 24 forks source link

how do i use it with shelf_router #50

Closed erf closed 3 years ago

erf commented 3 years ago

I tried the following:

  app.mount('/', createFileHandler('web/index.html'));

  app.mount('/web/<name|.*>', createStaticHandler('web'));

but i only get Not found.

alexobviously commented 3 years ago

@erf did you ever solve this? I'm trying to do the same thing to no avail

erf commented 3 years ago

Yes. Try this:

  final indexHandler = createFileHandler('web/index.html', url: '');
  final wsHandler = webSocketHandler(app.handle);
  final staticHandler = createStaticHandler('web');

  final router = Router();
  router.get('/', indexHandler);
  router.get('/ws', wsHandler);
  router.get('/<file|.*>', staticHandler);

  final handler = const shelf.Pipeline()
      .addMiddleware(shelf.logRequests())
      .addHandler(router);

  final server = await io.serve(handler, address.address, port);