尝试手动访问URL时出现错误404

时间:2019-04-23 16:26:15

标签: node.js reactjs express webpack-dev-server

我是网络开发的新手。 当我在本地运行项目(webpack-dev-server)时,我可以手动更改url并查看特定的组件。因此,在本地,一切正常,我将historyApiFallBack添加到我的webpack配置等中,并且效果非常完美。

但是,当我将构建文件上传到虚拟主机并且项目上线时,我无法像http://tizim.org.kz/about那样手动更改url或刷新。它为我提供了默认的404页面托管。 在下面,您可以查看我的配置和图像,该图像显示了如何管理应用程序目录。

任何相关的答案或建议都将非常有帮助。 谢谢您的关注。

enter link description here

this is a pic of my project directory

server.js

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

const app = express();

app.use(express.static(path.join(__dirname, "build")));

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

const port = process.env.PORT || 4444;
app.listen(port, () => console.log(`server running on port ${port}`));

webpack.config.js

const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  entry: path.join(__dirname, "src", "index.js"),
  output: {
    path: path.join(__dirname, "build"),
    filename: "bundle.js",
    publicPath: "/"
  },


  devServer: {
    historyApiFallback:true
    },


  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.(css|scss)$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          "postcss-loader",
          "sass-loader"
        ]
      },
      {
        test: /\.(jpg|jpeg|png|gif|mp3|svg)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[path][name]-[hash:8].[ext]"
            }
          }
        ]
      }
    ]
  },

  plugins: [
    new HtmlWebpackPlugin({
      filename: "index.html",
      template: path.join(__dirname, "src", "index.html")
    }),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
  ]
};

package.json

{
  "name": "webpack-babel-react-starter",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "@babel/core": "7.2.2",
    "@babel/plugin-proposal-class-properties": "7.2.3",
    "@babel/plugin-transform-runtime": "7.2.0",
    "@babel/preset-env": "7.2.3",
    "@babel/preset-react": "7.0.0",
    "autoprefixer": "9.4.3",
    "babel-loader": "8.0.4",
    "css-loader": "2.1.0",
    "file-loader": "3.0.1",
    "html-webpack-plugin": "3.2.0",
    "mini-css-extract-plugin": "0.5.0",
    "node-sass": "4.11.0",
    "postcss-loader": "3.0.0",
    "sass-loader": "7.1.0",
    "style-loader": "0.23.1",
    "webpack": "4.28.3",
    "webpack-cli": "3.1.2",
    "webpack-dev-server": "3.1.14"
  },
  "scripts": {
    "start": "node server.js",
    "dev": "webpack-dev-server --mode production --hot",
    "build": "webpack",
    "heroku-postbuild": "npm run build"
  },
  "dependencies": {
    "@babel/runtime": "7.2.0",
    "bootstrap": "^4.3.1",
    "react": "^16.8.6",
    "react-bootstrap": "^1.0.0-beta.8",
    "react-bootstrap-webpack": "^1.0.4",
    "react-dom": "^16.8.6",
    "react-router": "^5.0.0",
    "react-router-dom": "^4.3.1",
    "react-router-transition": "^1.3.0"
  },
  "browserslist": [
    "> 0.2%",
    "last 15 versions",
    "not dead"
  ]
}

0 个答案:

没有答案