繁体   English   中英

JS 和 Image 目录加载在本地主机中,但在部署 express 时不加载

[英]JS and Image directories loading in localhost but not when deployed express

我使用 nginx 作为 web 服务器,使用 cloudflare 作为 ddns。

JS 和图像目录似乎加载得很好localhost:5000/ 但是,当我在aarth.in上查看时,导演不见了。 有很多解决方案,但没有一个对我有用。 任何帮助,将不胜感激!

我的目录如下

- node_modules
- server.js
- public
    - index.html
    - images/
        - base.png
        - sub
    - scripts
        - base.js
        - sub
    - css
        - base.css
        - sub/
            - sub.css

服务器.js

const express = require('express');
const app = express();
app.use(express.static("public"));

app.get((req, res) => {
});

app.listen(5000, () => console.log('http://localhost:5000/'));

我在浏览器控制台中看到的 404 错误。

JS 的 404 错误

对于任何面临同样问题的人,结果证明server.js中没有错误。 我不得不更改 nginx 默认配置并注释掉根目录。

这是我的配置:

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

        #root /var/www/html; --> Commenting this did the trick!

        server_name example.com www.example.com;

        location / {
                proxy_pass http://localhost:YOUR_PORT;
                try_files $uri $uri/ =404;
        }

        #Nginx handles all the static files instead of node
        location ~^\/(images|css|fonts|js|icons|scss) {
           expires 1M;
           access_log off;
           add_header Cache-Control "public";
           add_header Access-Control-Allow-Origin *;
           add_header Access-Control-Allow-Methods GET;
           add_header Access-Control-Allow-Headers X-Requested-With,content-type;
           add_header Access-Control-Allow-Credentials true;
           root /home/pi/Documents/Website/public;
       }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM