首页 > 解决方案 > axios 获取请求返回我尝试服务的所有静态 html 文件的 vue index.html 文件

问题描述

所以,我有一个在我的 MAC 上运行良好的 vue 应用程序npm run serve

运行npm run build我的代码并将其部署到我的 CENTOS 环境(使用 NGINX),所有返回的 .get 请求都在网络选项卡中返回 200,但内容应该是我要拉入我的应用程序的静态 html 文件。

以下是一些代码示例:

this.axios.get(flatHtmlLocation).then(response => {
                let corsHTML = response.data;
                let htmlDoc = (new DOMParser()).parseFromString(corsHTML, "text/html");
                this.rawDog = htmlDoc;
                this.htmlData = htmlDoc.documentElement.getElementsByTagName('body')[0].innerHTML;
            })

^ 当我在我的 Mac 上玩耍时,这很好用。

我正在从特定目录中的 /public/ 目录中提取我的静态 .html 文件。

这是我的文件结构的样子:

应用程序名称

-- public
     index.html
     /directory-where-static-html-lives/filename.html 
  -- src
     -- components/component/file_pulling_the_static_asset.vue

这是我正在使用的产品中应用程序的 .conf (NGINX) 文件的副本:

    server {
    listen 80 default_server;
    listen [::]:80 default_server;


    server_name _;

    location /api/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_redirect off;
        proxy_buffering off;
        proxy_pass http://some-fancy-pants-url:4000;
    }
 location /public/ {
        root /opt/location-of-my-app;
    }

    location / {
        root    /opt/location-of-my-app/dist;
        index    index.html index.htm;
        include  /etc/nginx/mime.types;
        try_files $uri $uri/ /index.html;
    }



}

npm run build 后来自 axios GET 的 200 生产响应

200 响应的内容是静态的 Vue.js html 登陆页面

VUE /public/ html 登陆页面..

标签: node.jsvue.jsnginxaxios

解决方案


推荐阅读