我正在尝试添加 grunt-connect-proxy到一个使用最新的 yeoman + Angular 生成器的项目。
但是,当到达应该转发的路径时(例如 127.0.0.1:9000/api/v1),服务器返回此错误:
An error has occurred: {"code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect"}
我使用了修改后的 Gruntfile:same as here按照建议。
我应该如何调试它?有没有人最近遇到过与 grunt-connect-proxy 类似的问题?
我也对代理 angular webapp 的 api 调用的类似解决方案感兴趣,如果您有任何实际可行的话。
谢谢你帮助我!非常感谢!
最佳答案
Gruntfile 没问题,错误是由您的后端引起的,即 localhost:3000(可能是 Rails)。
如果是Rails,就是CORS问题。您需要允许 Rails 接受来自 grunt 服务器的连接。将“rack-cors”添加到您的 gemfile 并在您的 development.rb 配置文件中像这样配置它:
config.middleware.use Rack::Cors do
allow do
origins 'localhost:9000'
resource '*', :headers => :any, :methods => [:get, :post, :options, :delete]
end
end
查看此链接了解详细设置,它帮助我让它正常工作。
关于angularjs - 使用 grunt-connect-proxy 的 Yeoman 返回 ECONNREFUSED 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20092065/