0

我一直在尝试更改此 HTTPServer 的主机名,以便从网络上的任何机器访问它,但是该站点仍然只能通过 localhost 访问。我尝试将“0.0.0.0”主机名字符串解析为 InternetAddress,但这也不起作用。

import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'dart:convert';

void main(List<String> arguments) async {
  final app = Router();
  const _hostname = "0.0.0.0";
  const _port = 8080;

  app.get('/', (Request request) {
    var queryParameters = request.requestedUri.queryParameters;
    var error = false;
    var resultMessage = "";
    int x, y, z;

    print(request);

    if (queryParameters["x"] != null)
      try {
        x = int.parse(queryParameters["x"]);
      } on FormatException {
        resultMessage =
            resultMessage + "Please provide a numerical value for x.";
        error = true;
      }
    else {
      resultMessage = resultMessage + "Please provide a value for x.";
      error = true;
    }

    if (queryParameters["y"] != null)
      try {
        y = int.parse(queryParameters["y"]);
      } on FormatException {
        resultMessage =
            resultMessage + "Please provide a numerical value for y.";
        error = true;
      }
    else {
      resultMessage = resultMessage + "Please provide a value for y.";
      error = true;
    }

    if (error) {
      var test = {"error": true, "string": resultMessage, "answer": 0};
      //var resp = new Response(400);
      return Response.ok(json.encode(test));
    } else {
      z = (x ~/ y);
    }

    var test = {"error": false, "string": "${x} / ${y} = ${z}", "answer": 0};
    return Response.ok(
      json.encode(test),
      headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
      },
    );
  });

  await io.serve(app.handler, _hostname, _port);
}

请原谅代码的幼稚,因为这是我第一次使用飞镖。

编辑:更多信息 - IDE 是 VS Code,Kali linux 在命令行中执行。我的计划是将其容器化并将其部署到 Kubernetes 集群上,仅供参考。

4

0 回答 0