简体   繁体   English

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

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

I am using nginx as web server with cloudflare as ddns.我使用 nginx 作为 web 服务器,使用 cloudflare 作为 ddns。

The JS and image directory seems to load up just fine localhost:5000/ . JS 和图像目录似乎加载得很好localhost:5000/ However, the directors are missing when I view it on aarth.in .但是,当我在aarth.in上查看时,导演不见了。 There are many solution on such but none of them worked for me.有很多解决方案,但没有一个对我有用。 Any help would be appreciated!任何帮助,将不胜感激!

My directory is as follow我的目录如下

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

server.js服务器.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 errors I see in browser console.我在浏览器控制台中看到的 404 错误。

JS 的 404 错误

For anyone facing the same issue, it turns out there was no error in server.js .对于任何面临同样问题的人,结果证明server.js中没有错误。 I had to change the nginx default config and comment out the root.我不得不更改 nginx 默认配置并注释掉根目录。

Here is my config:这是我的配置:

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.

相关问题 图像未在 Express JS 中加载 - Image not loading in Express JS 节点服务器(不带 Express)不渲染图像,它继续在本地主机加载 - node server (without Express) does not render image and it keeps loading at localhost 从本地主机加载时图像闪烁 - Image blinking when loading from localhost CSS 和 JS 文件离线加载,但在 Heroku 上部署时不加载 - CSS and JS files loading offline but not when deployed on Heroku API 在 localhost 中运行,但在部署时没有 - API running in localhost but not when deployed Passport.Js 无法从客户端获取某些浏览器的会话 - 在 localhost 上完美运行,但在部署时无法正常工作 - Passport.Js not able to get session from client for some browsers - works perfectly on localhost but not when deployed 无法使用localhost node.js / express.js服务器加载html图像 - Cannot load html image using localhost node.js/express.js server 在Express中加载CSS和JS时遇到500内部服务器错误 - Run into a 500 internal server error when loading css and js in express 将Vue.js PWA插件部署到子文件夹时(Github页面)不加载service-worker.js - Vue.js PWA Plugin not loading service-worker.js when deployed to subfolder (Github Pages) Express.js应用程序在本地可以正常运行,但是在Heroku上部署时无法正常工作 - Express.js app works fine locally but doesn't work when deployed on Heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM