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



目录
相关文章
|
2月前
|
机器学习/深度学习 前端开发 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
43 0
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
|
4月前
|
开发框架 .NET 数据库连接
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
719 0
|
5月前
|
JSON 小程序 前端开发
小程序踩坑-http://xxx.com 不在以下 request 合法域名列表中
小程序踩坑-http://xxx.com 不在以下 request 合法域名列表中
132 0
|
2月前
|
存储 Web App开发 缓存
【JavaEE初阶】 HTTP 请求 (Request)详解
【JavaEE初阶】 HTTP 请求 (Request)详解
|
6月前
|
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处理方案
|
2月前
|
小程序
报错:http://edu.newsight.cn不在以下request合法域名列表中,请参考文档
报错:http://edu.newsight.cn不在以下request合法域名列表中,请参考文档
HTTP request以及response原理 request请求消息数据
HTTP request以及response原理 request请求消息数据
HTTP request共享数据
HTTP request共享数据
Http 实现用户登录(mysql+html+request)
Http 实现用户登录(mysql+html+request)