Proxy error: Could not proxy request xxx from localhost:8080 to http://localhost:3000(ECONNREFUSED)

简介: Proxy error: Could not proxy request xxx from localhost:8080 to http://localhost:3000(ECONNREFUSED)

问题

mock 数据的时候,配置 vue.config.js 里的 devServer 的 proxy 时报错,错误如下image.png我的配置如下:

module.exports = {
    devServer: {
        proxy: {
            '/api': {
                target: 'http://localhost:3000',
                bypass: function (req, res, proxyOptions) {
                    if (req.headers.accept.indexOf('html') !== -1) {
                        console.log('Skipping proxy for browser request.');
                        return '/index.html';
                    } else {
                        // 将请求url转为文件名
                        const name = req.path.split("/api/")[1].split("/").join("_");
                        const mock = require(`./mock/${name}`);
                        const result = mock(req.method);
                        // 需要清除缓存
                        delete require.cache[require.resolve(`./mock/${name}`)];
                        return res.send(result);
                    }
                },
            },
        },
    },
}



解决方案一


我们可以将 target 端口 3000 改成跟起的服务一样 http://localhost:8080,都是 8080 端口即可。

module.exports = {
    devServer: {
        proxy: {
            '/api': {
                target: 'http://localhost:8080',
                bypass: function (req, res, proxyOptions) {
                    if (req.headers.accept.indexOf('html') !== -1) {
                        console.log('Skipping proxy for browser request.');
                        return '/index.html';
                    } else {
                        // 将请求url转为文件名
                        const name = req.path.split("/api/")[1].split("/").join("_");
                        const mock = require(`./mock/${name}`);
                        const result = mock(req.method);
                        // 需要清除缓存
                        delete require.cache[require.resolve(`./mock/${name}`)];
                        return res.send(result);
                    }
                },
            },
        },
    },
}

d16d9ed026d045c89dd475838c1dbe26.pngECONNREFUSED 的意思就是:ECONNREFUSED(连接被拒绝):无法建立连接,因为目标机器主动拒绝了它。这通常是由于尝试连接到在外部主机上不活动的服务造成的。上面报错就是因为代理对象没有开启服务,不能访问到http://localhost:3000 对象服务器。


ECONNREFUSED的说明



解决方案二


就是修改代理规则 /api@(/api),至于原因我也不清楚,有知道的大佬还望告知一下,在此先感谢。

module.exports = {
    devServer: {
        proxy: {
            '@(/api)': {
                target: 'http://localhost:3000',
                bypass: function (req, res, proxyOptions) {
                    if (req.headers.accept.indexOf('html') !== -1) {
                        console.log('Skipping proxy for browser request.');
                        return '/index.html';
                    } else {
                        // 将请求url转为文件名
                        const name = req.path.split("/api/")[1].split("/").join("_");
                        const mock = require(`./mock/${name}`);
                        const result = mock(req.method);
                        // 需要清除缓存
                        delete require.cache[require.resolve(`./mock/${name}`)];
                        return res.send(result);
                    }
                },
            },
        },
    },
}

配置完之后也没有报错

84b11bde47f1432ea905e85e1de951be.png



目录
相关文章
|
1月前
|
机器学习/深度学习 前端开发 JavaScript
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
62 0
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
|
13天前
|
Kubernetes 容器 Perl
k8s部署seata 报错 没有提供足够的身份验证信息 [ http-nio-7091-exec-2] [ty.JwtAuthenticationEntryPoint] [ commence] [] : Responding with unauthorized error. Message - Full authentication is required to access this resource
Kubernetes pod 在16:12时出现两次错误,错误信息显示需要完整认证才能访问资源。尽管有此错误,但页面可正常访问。附有yaml配置文件的图片。
28 2
|
1月前
|
JSON API 开发者
什么是 HTTP 400 bad request 错误
什么是 HTTP 400 bad request 错误
162 1
|
1月前
|
存储 Web App开发 安全
如何处理错误消息 HTTP error 421 occurred SSL handshake with github.com
如何处理错误消息 HTTP error 421 occurred SSL handshake with github.com
21 0
|
1月前
|
开发框架 .NET 数据库连接
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
1178 0
|
7月前
|
JSON 小程序 前端开发
小程序踩坑-http://xxx.com 不在以下 request 合法域名列表中
小程序踩坑-http://xxx.com 不在以下 request 合法域名列表中
145 0
|
8月前
|
Java 容器
RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out问题处理
讲述RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out处理方案
|
1月前
|
存储 Web App开发 缓存
【JavaEE初阶】 HTTP 请求 (Request)详解
【JavaEE初阶】 HTTP 请求 (Request)详解
|
1月前
|
小程序
报错:http://edu.newsight.cn不在以下request合法域名列表中,请参考文档
报错:http://edu.newsight.cn不在以下request合法域名列表中,请参考文档
HTTP request以及response原理 request请求消息数据
HTTP request以及response原理 request请求消息数据