gpt4 book ai didi

Node.js react 提供不正确的 Assets URL

转载 作者:行者123 更新时间:2023-12-03 12:18:41 26 4
gpt4 key购买 nike

我在解决我的 Create React App 路径时遇到了 Node 问题。

问题:

Assets (chunk.js) files are resolving to relative path rather than absolute path.



当我从根文件夹(example.com)访问网站并点击 /games/网址工作正常。但是,如果我刷新,它会附加 /games进入网址。

例如:
http://movies-finder.surge.sh/movies/419704

^ 访问页面并刷新页面。

正确链接:

https://example.com/static/js/main.b9f8ee12.chunk.js



错误链接: (这是在用户刷新页面时发生的)

https://example.com/games/static/js/main.e50e1c49.chunk.js



我只是想确定一下,我没有遇到 /games当我的 Assets 被访问时。

(不需要 /games/ )因此坏了:(

文件夹结构:
-/
- server.js
- public
- index.html
- package.json
- Build

Package.json:
{
"name": "games-finder",
"version": "0.1.2",
"private": true,
"homepage": ".",
"proxy": "http://localhost:3001/",
"dependencies": {
// dependencies
},
"devDependencies": {
// dev dependencies for the project.
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"server": "node-env-run server.js --exec nodemon | pino-colada",
"dev": "run-p server start",
"heroku-dev": "run-p server start"
}
}

server.js:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 3001;
const path = require('path');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(pino);

app.get('/api/greeting', (req, res) => {
const name = req.query.name || 'World';
res.send(JSON.stringify({ greeting: `Hello ${name}!` }));
});

if (process.env.NODE_ENV === 'production') {

// Serve any static files
app.use(express.static('build'));

// Handle React routing, return all requests to React app
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
}

app.listen(port, () => console.log(`Listening on port ${port}`));

请帮我找出问题所在。我花了几天时间试图调试这个问题。

我只是想确定一下,我没有遇到 /games当我的 Assets 被访问时。

最佳答案

这是因为 "homepage": "."在您的 package.json 中,尝试删除这一行。

详细说明在这里https://stackoverflow.com/a/58508562/9173730

关于Node.js react 提供不正确的 Assets URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62166483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com