确认后端服务是否正常运行:使用curl或Postman等工具测试API接口是否可用。如果无法获取预期的响应,则需要检查后端服务(Golang)是否正确运行。
确认nginx配置文件是否正确:如果nginx配置文件中的代理设置不正确,则会阻止Angular应用程序与Golang服务进行通信。可以对配置文件进行以下更改:
server {
listen 80;
server_name example.com
location /api/ {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
以上配置文件中,/api/代表后端服务的端口号,http://localhost:8080/指向Golang应用程序的API端口。
@Injectable()
export class TaskService {
private baseUrl = "http://localhost:8080/api/tasks";
constructor(private http: HttpClient) {}
// ...
}
以上代码中,baseUrl的端口号与nginx代理和Golang应用程序中的API端口一致。
func main() {
r := gin.Default()
r.Use(cors.Default())
// ...
}
以上代码中,cors.Default()会为应用程序添加CORS支持。
通过实施以上步