1

Ich möchte einen Proxy-Server für meine eckigen Dart-Anwendungen erstellen, die mich zu index.html immer umleitet, wenn ich den Browser aktualisiere und die Route unterscheidet sich von root. Zum Beispiel, wenn die URL ist: http://localhost:8080/users und ich drücken Sie die Schaltfläche Aktualisieren, würde ich gerne umgeleitet werden zu http://localhost:8080 oder http://localhost:8080/index.html.Angular Dart - Wie erstelle ich einen Proxy-Server für eckige Dart-Apps?

Darüber hinaus wird es sehr nützlich sein, eine Verbindung zu einer Drittanbieter-Server-Anwendung herstellen zu können. Mit anderen Worten, würde Ich mag pub serve und Server eines Dritten (Python, PHP oder Java) laufen auf localhost:9000 läuft

Antwort

2

zu tun, dass wir nächsten Code verwenden:

import 'dart:convert'; 
import 'dart:io'; 
import 'package:shelf/shelf.dart'; 
import 'package:shelf/shelf_io.dart'; 
import 'package:shelf_proxy/shelf_proxy.dart'; 
import 'package:shelf/src/message.dart' show getBody; 

main() async { 
    _startPubServe(); 
    var server = await serve(handler, 'localhost', 9080); 
    print('Proxying at http://${server.address.host}:${server.port}'); 
} 

handler(Request request) { 
    // redirects all the `api` calls to the third party server 
    print('request.url.path: ${request.url.path}'); 
    if (request.url.path.startsWith('api')) { 
    print('proxying to: http://localhost:3333/api'); 
    return proxyHandler('http://localhost:3333/api')(request); 
    } 

    // redirects all files to default `pub serve` path 
    var handler = proxyHandler('http://localhost:8080'); 
    if (new RegExp(r'\.(css|dart|html|png|ttf|otf|TTF)$').hasMatch(request.url.path)) { 
    print('proxyiing to: http://localhost:8080'); 
    return handler(request); 
    } 

    // redirect all the routes to `index.html` 
    print('proxying to: http://localhost:8080/index.html'); 
    return handler(new Request(
     request.method, 
     Uri.parse('http://localhost:8080/index.html'), 
     protocolVersion: request.protocolVersion, 
     headers: request.headers, 
     handlerPath: request.handlerPath, 
     body: getBody(request), 
     encoding: request.encoding, 
     context: request.context)); 
} 

// starts `pub serve` when running this server 
_startPubServe() async { 
    String executable = Platform.isWindows ? 'pub.bat' : 'pub'; 
    var process = await Process.start(executable, ['serve', '--port', '8080']); 
    process.stdout.transform(UTF8.decoder).listen(print); 
} 
Verwandte Themen