dart-lang / shelf

Web server middleware for Dart
https://pub.dev/packages/shelf
BSD 3-Clause "New" or "Revised" License
921 stars 124 forks source link

Different folders on different routes #418

Closed develocode777 closed 7 months ago

develocode777 commented 7 months ago

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}');
}
kevmoo commented 7 months ago

Pipeline().addHandler( is not needed. It's a bit redundant.

I should absolutely be possible to do what you're doing.

develocode777 commented 7 months ago

Pipeline().addHandler( is not needed. It's a bit redundant.

I should absolutely be possible to do what you're doing.

But it does not work, can you please provide an example?

kevmoo commented 7 months ago

Does it work with one?

develocode777 commented 7 months ago

I tried with this code:

 final ip = InternetAddress.anyIPv4;

  final router = Router()
    ..get('/one', createStaticHandler('../one', defaultDocument: 'index.html'))
    ..get('/two', createStaticHandler('../two', defaultDocument: 'index.html'));

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

on both routes it says "Not Found", if instead of ..get('/one', I do ..get('/', then it works for one not for two

develocode777 commented 7 months ago

Actually even for a single folder it does not work if route is other than /

 final ip = InternetAddress.anyIPv4;

  final router = Router()
    ..get(
        '/one',
        createStaticHandler('../one',
            defaultDocument: 'index.html'));

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

Maybe go to

https://discord.com/channels/420324994703163402/825210836003323904

develocode777 commented 7 months ago

Maybe go to

https://discord.com/channels/420324994703163402/825210836003323904

I don't have access on that link

kevmoo commented 7 months ago

Start here: https://discord.gg/rflutterdev