提问者:小点点

节点。js react提供不正确的资产url


我一直在遇到节点问题,以解决我的创建反应应用程序的路径。

问题:

资产(chunk.js)文件正在解析为相对路径而不是绝对路径。

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

例如:

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

(不需要/games/)因此被破坏:(

文件夹结构:

-/
 - server.js
 - public
    - index.html
 - package.json
 - Build

包裹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"
  }
}

服务器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


共1个答案

匿名用户

这是因为“主页”:在您的包中。json,请尝试删除此行。

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