[英]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 错误。
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.