简体   繁体   English

对JS和CSS的请求改为提供index.html

[英]Requests for JS and CSS giving index.html instead

I'm using Express and create-react-app . 我正在使用Expresscreate-react-app

My React app is a ways along, and now I'm trying to serve it from an Express server. 我的React应用程序是一种发展的方式,现在我正在尝试通过Express服务器为其提供服务。

// server/app.js

const express = require('express');
const path = require('path');

const app = express();

// Serve static assets

app.use(express.static(path.resolve(__dirname, '..', 'build')));

// serve main file

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});


module.exports = app;

(My build directory is populated when I do npm run build . (当我执行npm run build时,将填充我的构建目录。

I view the page in Chrome, and what happens when loading the page at localhost:3000 is the Console prints Uncaught SyntaxError: Unexpected Token < , and in the Sources tab it shows that the content of my CSS and JS files are simply the same as index.html : as in this image. 我在Chrome中查看该页面,并且在localhost:3000加载该页面时会发生什么,就是控制台打印出Uncaught SyntaxError: Unexpected Token < ,并且在Sources选项卡中它显示我的CSS和JS文件的内容与index.html :如该图所示。

HTML是否覆盖了我的JS和CSS?

This seems like a recognizable issue, so hopefully someone has seen this before. 这似乎是一个可以识别的问题,所以希望有人以前见过。 I'm sort of stumped on where to even begin, especially because I was actually serving the app from Express successfully at first. 我对从何处开始感到困惑,尤其是因为起初我实际上是从Express成功提供应用程序的。 Then this started happening, then it stopped after some random switching of git branches and reverting and replaying changes, and then it started happening again. 然后开始发生这种情况,然后在随机切换git分支并还原和重播更改后停止,然后再次开始发生。 So I'm not even sure what makes it happen or not happen. 所以我什至不知道是什么使它发生或不发生。

It appears that your app.use(express.static... call is failing, so instead all of the requests (including for the static assets) are being handled by the app.get('*', (req, res) => { part. 您的app.use(express.static...调用似乎失败了,因此所有请求(包括对静态资产的请求)都由app.get('*', (req, res) => {部分。

As you are intending to use this to serve a React app, I'd suggest taking inspiration from a boilerplate, "to see how it's done". 当您打算使用它来服务React应用程序时,我建议从样板中汲取灵感,“看看它是如何完成的”。 I personally use NYTimes's kyt project and there's react-starter-kit too. 我个人使用NYTimes的kyt项目,也有react-starter-kit。

Try the following code changes which are detailed from the express documentation - serving static files in express : 请尝试以下代码更改,这些更改在express文档中有详细介绍-在express中提供静态文件

Replace 更换

app.use(express.static(path.resolve(__dirname, '..', 'build')));

With

app.use(express.static('build'))

Remove 去掉

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});

The problem was "homepage": ... in package.json . 问题是"homepage": ...package.json

When npm run build runs and there is a homepage URL in package.json that has a non-empty path (like a Github Pages URL like this, https://username.github.io/project_name , where "/project_name" is the path), it changes where it expects the files inside /build to be. npm run build运行时,package.json中有一个主页URL,该主页URL具有非空路径(例如像这样的Github Pages URL, https ://username.github.io/project_name,其中“ / project_name”是路径),它会更改期望/build文件的位置。 The requests for my js and css were going to /project_name/static/... instead of /static/... . 对我的js和CSS的请求将转到/project_name/static/...而不是/static/...

It even said in the log output of npm run build : 它甚至在npm run build的日志输出中说:

The project was built assuming it is hosted at /project_name/.
You can control this with the homepage field in your package.json.

On my localhost, it wasn't hosted at /project_name/ , so the paths were off. 在我的本地主机上,它没有托管在/project_name/ ,因此路径已关闭。

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

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