【发布时间】:2019-12-29 15:49:45
【问题描述】:
我在开发全栈应用程序方面已经工作了大约 2 年(当我最初接触这些应用程序时,这些应用程序已经相当成熟),并且正在从头开始我的第一个应用程序。我花了一天时间创建一个 EC2 实例,为我的堆栈加载依赖项(Postgres、golang、react),以及一台闪亮的新机器所需的所有其他装饰(tmux、npm、vim 插件、git 集成等)。我现在卡住了。 我使用 create-react-app 开始我的前端并运行 npm start 进行构建。要看到这个,我可以去我的 instanceip:3000。 我创建了在 8080 端口上提供服务的 golang 服务器代码,我可以通过访问 instanceip:8080/hello 来访问我的 helloworld 代码。 我在我的反应代码中添加了一个 axios 调用来获取返回 404 的端点 /hello。
我还尝试让我的 golang 将 index.js 作为 instanceip:8080 的静态页面处理,并返回 404。
如何让我的不同端口玩得更好?我无法弄清楚我在这里缺少什么。
这是我在 main 中的服务器代码的 sn-p:
indexLoc := os.Genenv(webRootEnv) + "/index.html" // verified this is the correct path to my static index.html file by logging it out
fs := http.FileServer(http.Dir(indexLoc))
http.ListenAndServe(":8080", nil)
http.Handle("/", fs)
有人知道我缺少什么吗?提前致谢!
【问题讨论】:
-
您将文件 (
index.html) 的路径提供给http.Dir,该文件需要指定一个目录名称。也许这可能是原因?另外,你应该在ListenAndServe之前调用http.Handle
标签: amazon-web-services go server