[英]React App giving 404 on main js and css
I built a react app using "react-scripts". 我使用“react-scripts”构建了一个react应用程序。 The application runs perfectly on my local development server but when I deploy to my actual server the applications seems to not find the main JS and CSS files being compiled.
该应用程序在我的本地开发服务器上运行完美,但是当我部署到我的实际服务器时,应用程序似乎找不到正在编译的主要JS和CSS文件。 I get 404 on both.
我两个都得到404。
Following is the information that might help. 以下是可能有用的信息。 The files on the server are located at
服务器上的文件位于
ads/build/static/js and ads/build/static/css ||
ads / build / static / js和ads / build / static / css || respectively
分别
The 404s I am getting are on the following files: 我得到的404s是在以下文件:
https://www.example.com/ads/build/static/css/main.41938fe2.css https://www.example.com/ads/build/static/js/main.74995495.js
https://www.example.com/ads/build/static/css/main.41938fe2.css https://www.example.com/ads/build/static/js/main.74995495.js
Here is how my server is configured: 以下是我的服务器配置方式:
const express = require('express');
const path = require('path');
const app = express();
const favicon = require('serve-favicon');
//favicon
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.get('/ads', function (req, res) {
app.use(express.static(path.join(__dirname, 'build/static')));
console.log(path.join(__dirname, 'build'));
res.sendFile(path.join(__dirname, '/build/index.html'));
});
app.listen(9000);
In my package.json I have also included the homepage parameter as: "homepage" : "https://www.example.com/ads
在我的package.json中,我还将主页参数包含为:
"homepage" : "https://www.example.com/ads
UPDATE When I run the app on the server itself with the following path: dedicated-server-host:9000/static/js/main.74995495.js
that renders the JS file correctly 更新当我使用以下路径在服务器上运行应用程序时:
dedicated-server-host:9000/static/js/main.74995495.js
正确呈现JS文件
Is there some configuration that I am missing, the routing doesn't seem to be working. 是否有一些我缺少的配置,路由似乎没有工作。 Please advise.
请指教。
Use some indentation so you will see error like this: 使用一些缩进,这样你会看到如下错误:
app.get('/ads', function (req, res) {
app.use(express.static(path.join(__dirname, 'build/static')));
console.log(path.join(__dirname, 'build'));
res.sendFile(path.join(__dirname, '/build/index.html'));
});
You are setting the static route inside of the /ads handler, will add a new express.static
route handler on every GET /ads request. 您正在/ ads处理程序中设置静态路由,将在每个GET /广告请求中添加一个新的
express.static
路由处理程序。
This will set the static route on server startup: 这将在服务器启动时设置静态路由:
app.use(express.static(path.join(__dirname, 'build/static')));
app.get('/ads', function (req, res) {
console.log(path.join(__dirname, 'build'));
res.sendFile(path.join(__dirname, '/build/index.html'));
});
or: 要么:
app.get('/ads', function (req, res) {
console.log(path.join(__dirname, 'build'));
res.sendFile(path.join(__dirname, '/build/index.html'));
});
app.use(express.static(path.join(__dirname, 'build/static')));
But make sure that you get the path right - for example you may need: 但请确保您获得正确的路径 - 例如您可能需要:
app.use('/ads/build/static', express.static(path.join(__dirname, 'build/static')));
if you want the URL in your question to work. 如果您希望问题中的URL有效。
To make it much simpler, you could use just this single handler to make express.static handle both the css/js and index.html at the same time: 为了使它更简单,你可以使用这个单独的处理程序使express.static同时处理css / js和index.html:
app.use('/ads', express.static(path.join(__dirname, 'build')));
and change your index.html to use: 并将index.html更改为使用:
instead of: 代替:
Sometimes getting your paths structure right in the first place can make your route handlers much easier. 有时,首先将路径结构设置为正确可以使您的路径处理程序更加容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.