我正在开发一个 Nativescript 移动应用程序,同时尝试设计一个 Golang 后端。我没有实际部署后端的经验,所以现在我只是想在本地开发。所以我的模拟应用程序上有一些按钮:
import { Http } from "@angular/http"
+@Component({...}
+export class LoginComponent implements OnInit {...}{
. . .
// <Button ... (tap)="get_test()"></Button>
public get_test(){
this.http.get("http://localhost:8080/");
}
}
还有一个我从 golang website 复制的小型网络服务器:
package main
import (
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
print("recieved")
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
我启动go
“服务器”:
go build main.go
./main
现在我的想法是,通过单击按钮,命令行应该打印出 recieved
,我可以使用它来设计我的请求。然而,双方都没有任何反应。代码中可能有很多错误,我什至不确定您是否可以在本地进行模拟。但我们将不胜感激。
最佳答案
您的服务器端代码没有任何问题。在我看来,这听起来像是 (a) 您的前端代码实际上并未触发 http 请求(您应该能够通过附加到浏览器的调试器来检测到)或 (b) 您的前端正在尝试发出请求但没有不知道如何找到服务器(即网络问题)。
关于angular - 为 Nativescript 移动应用程序在本地开发 Golang 后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49826426/