node.js - webpack-dev-server配置proxy一直报502

 孙衍龙 发布于 2022-11-01 13:42

这是webpack-dev-server的配置:

var server = new WebpackDevServer(webpack(config), {
    publicPath: 'http://localhost:9999/',
    hot: true,
    prependPath: false,
    historyApiFallback: true,
    inline: true,
    progress: true,
    stats: {
        colors: true
    },
    proxy: {
        '/api/*': {
            target: 'http://www.weather.com.cn',
            secure: false,
            pathRewrite: {'^/api': ''}
        }
    }
});

这是ajax请求:

$.ajax({
    url: '/api/data/sk/101010100.html',
    type: 'get',
    dataType: 'json',
    success: function (data) {
        console.log(data);
    }
});

这是浏览器请求的信息:

Request URL:http://localhost:9999/api/data/sk/101010100.html
Request Method:GET
Status Code:502 Bad Gateway
Remote Address:[::1]:9999
Response Headers
view source
Connection:keep-alive
Content-Length:0
Date:Fri, 10 Mar 2017 06:56:20 GMT
X-Powered-By:Express

请问哪里配置错了吗

1 个回答
  • 尝试让webpak-dev-server 代理的请求加上 源host

    proxy: {
        // 尝试修改下匹配 
        '/api': {
            target: 'http://www.weather.com.cn',
            secure: false,
            // 新增一行
            changeOrigin: true,
            pathRewrite: {'^/api': ''}
        }
    }
    2022-11-02 08:34 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有