1

shelf在 Dart 中使用来设置一个简单的 Web 服务器。在下面的示例中,如何将请求从/to重定向/oauth2callback

我找到了这种方法,但不知何故,即使我导入,我也无法编译代码dart:io

import 'dart:io';

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

final router = Router()
  ..get('/', rootHandler)
  ..get('/oauth2callback', oauth2callback);

Response rootHandler(Request req) {
  return Response.ok('Hello, World!\n');
}

Response oauth2callback(Request request) {
  return Response.ok('Redirected!\n');

}

void main(List<String> args) async {
  final _handler = Pipeline().addMiddleware(logRequests()).addHandler(router);
  final server = await serve(_handler, '0.0.0.0', 8080);
  print('Server listening on port ${server.port}');
}
4

1 回答 1

1

您可以像这样发送 302 响应:

Response rootHandler(Request request) {
  Map<String,String> headers = {'location':'/oauth2callback'};
  return Response(302,headers:headers);
});
于 2021-10-20T21:47:32.443 回答