Node.js React 提供不正确的资产 url
Node.js react serves incorrect asset url
我在解决我的 Create React App 路径时遇到了节点问题。
问题:
Assets (chunk.js) files are resolving to relative path rather than
absolute path.
当我从根文件夹 (example.com) 访问网站并点击 /games/
URL 时,它工作正常。但是,如果我刷新,它会将 /games
附加到 URL.
例如:
http://movies-finder.surge.sh/movies/419704
^访问页面并刷新页面
正确 Link:
不正确Link:(用户刷新页面时出现这种情况)
我只是想确定一下,当我的资产被访问时,我不会遇到 /games
。
(不需要 /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
。
发生这种情况是因为您的 package.json 中有 "homepage": "."
,请尝试删除此行。
此处有详细说明
我在解决我的 Create React App 路径时遇到了节点问题。
问题:
Assets (chunk.js) files are resolving to relative path rather than absolute path.
当我从根文件夹 (example.com) 访问网站并点击 /games/
URL 时,它工作正常。但是,如果我刷新,它会将 /games
附加到 URL.
例如:
http://movies-finder.surge.sh/movies/419704
^访问页面并刷新页面
正确 Link:
不正确Link:(用户刷新页面时出现这种情况)
我只是想确定一下,当我的资产被访问时,我不会遇到 /games
。
(不需要 /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
。
发生这种情况是因为您的 package.json 中有 "homepage": "."
,请尝试删除此行。
此处有详细说明