英文:
GO - html file can't load external js file
问题
我有一个使用HTML编写的GO项目。在HTML文件中,我已经编写了JavaScript,并且可以成功安装GO并加载UI。
问题是当我将这些JavaScript代码移动到一个外部文件中,并包含它时:。我只能构建GO,但在加载UI时,它会出现404 Not Found错误,即"myscripts.js"文件。奇怪的是错误的头部信息:
远程地址:127.0.0.1:8080
请求URL:http://localhost:8080/subscribe/myscripts.js
请求方法:GET
状态码:404 Not Found
请帮忙,我还没有找到任何关于这个问题的答案。
英文:
I have a GO project which has UI written by html. In HTML file, I have written javascript, and I can install GO and load the UI successfully.
The problem is when I move those javascript code to an external file, and include it: <script src="myscripts.js" type="text/javascript"></script>. I only can build GO, but when load the UI, it has error 404 Not Found which is "myscripts.js" file. The weird thing is on its header of error:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/subscribe/myscripts.js
Request Method:GET
Status Code:404 Not Found
Please help, I can't find any answer yet for this
答案1
得分: 3
如果你只为你的端点定义了处理程序,那是正常的。
你应该在你的路由器中添加 http.FileServer
处理程序,指向存储你项目文件的目录。
此外,考虑使用 <base>
来定义相对链接的根目录,这将防止路径嵌套等错误。
英文:
That's normal if you only define handler for your endpoints.
You should add to your router the http.FileServer
handler pointing to the directory that store your project files.
Additionally, think about the <base>
that to define the root of your relative links, which will prevent errors such as path nesting.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论