简体   繁体   English

无法从Web应用程序连接到localhost:3000

[英]Failure to connect to localhost:3000 from web application

I'm working on some boilerplate code to get a VueJS frontend working with a Go-powered backend (using gorilla/mux for a router): 我正在处理一些样板代码,以使VueJS前端与Go供电的后端一起工作(使用gorilla/mux作为路由器):

Currently, I seem to have everything working for the most part. 目前,我似乎大部分都在工作。 I'm serving a static HTML file via Go and rendering Vue stuff with it that's being bundled with webpack (webpack-dev-server currently, not a physical bundle), and I'm having a weird issue: 我正在通过Go提供一个静态HTML文件,并使用与Webpack捆绑在一起的Vue东西(目前是webpack-dev-server,而不是物理捆绑包)来呈现Vue的东西,但是我遇到了一个奇怪的问题:

In my browser, Firefox keeps telling me the following: 在我的浏览器中,Firefox不断告诉我以下内容:

Firefox can't establish a connection to the server at http://localhost:3000/__webpack_hmr.

Yet, when I change Vue components and save them, they're auto-refreshing in the browser as I would expect. 但是,当我更改Vue组件并保存它们时,它们会像我期望的那样在浏览器中自动刷新。 I'm really confused, I guess, I don't know why this is happening. 我真的很困惑,我想,我不知道为什么会这样。

I've tried changing the port that my Go server operates on, and it always seems to fail connection at http://localhost:<Go Server Port>/__webpack_hmr . 我尝试更改Go服务器运行的端口,但它似乎总是无法通过http://localhost:<Go Server Port>/__webpack_hmr

Any thoughts? 有什么想法吗? Maybe there's a step I'm missing or the setup is more complicated than I thought it was. 也许我缺少一个步骤,或者设置比我想象的要复杂。

Do I need to be running an Express server and using the webpack-based Middleware to fix this error? 我是否需要运行Express服务器并使用基于Webpack的中间件来解决此错误? If so, that sounds silly if I'm using Go for the backend (why would I run two servers?). 如果是这样,如果我在后端使用Go,那听起来很愚蠢(为什么要运行两台服务器?)。 Am I doing something wrong? 难道我做错了什么?

As for the Go side of things, I've tried both running the Go server on a different port and having the webpack server run on different ports and such, but it always fails to connect at http://localhost:<Go Server Port>/__webpack_hmr . 至于Go方面,我曾尝试在不同的端口上运行Go服务器,并在不同的端口上运行webpack服务器,但它始终无法通过http://localhost:<Go Server Port>/__webpack_hmr

Maybe this is more complicated to set up than I thought it was, or since it seems to be "working", I can safely ignore this error? 也许设置起来比我想象的要复杂,或者因为它似乎正在“起作用”,所以我可以放心地忽略此错误? I dunno. 我不知道。

Remove 'webpack-hot-middleware/client' from your webpack config and any references in your codebase. 从您的webpack配置和代码库中的所有引用中删除'webpack-hot-middleware/client' This is only used for hot reloading, so it's safe to remove it. 这仅用于热重装,因此可以安全删除。

Since you need hot reloading, you should use: https://github.com/go-webpack/webpack 由于需要热重装,因此应使用: https : //github.com/go-webpack/webpack

This module allows proper integration with webpack, with support for proper assets reloading in development and asset hashes for production caching. 该模块允许与webpack正确集成,并支持在开发中重新加载正确的资产,并为生产缓存提供资产哈希。

However they only show examples using Gin and Iris , so you may need to open an issue with them for an example based on purely net/http (assuming that's what you're using). 但是,它们仅显示使用GinIris示例,因此对于纯基于net/http的示例,您可能需要向它们打开一个问题(假设这就是您所使用的)。

if I understand your issue correctly. 如果我正确理解您的问题。 your client and server use a different port and if it is the case then you're using CORS(Cross-Origin Resource Sharing) request to communicate with client-server. 您的客户端和服务器使用不同的端口,如果是这种情况,则您正在使用CORS(跨原始资源共享)请求与客户端-服务器进行通信。 see the following links hope it will help you to fix CORS issue(It helps me a lot) 请参阅以下链接,希望它能帮助您解决CORS问题(对我有很大帮助)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM