首页 文章

使用webpack构建应用程序后,React Routes无法正常工作

提问于
浏览
2

使用webpack构建应用程序后,React Routes无法正常工作

当我使用webpack服务器运行应用程序时,路由工作正常(npm start)

webpack.config file

var webpack = require('webpack');

module.exports = {
    entry: {
        'index': [
            'webpack-dev-server/client?http://localhost:8080/',
            'webpack/hot/only-dev-server',
            './index.jsx'
        ]
    },
    output: {
        path: __dirname,
        filename: "[name].js",
        publicPath: 'http://localhost:8881/',
        chunkFilename: '[id].chunk.js',
        sourceMapFilename: '[name].map'
    },
    resolve: {
        extensions: ['', '.js', '.jsx', '.es6'],
        modulesDirectories: ['node_modules']
    },
    module: {
        loaders: [
            {test: /\.jsx$|\.es6$|\.js$/, loaders: ['react-hot', 'babel-loader'], exclude: /node_modules/},
            {test: /\.scss$|\.css$/, loader: 'style-loader!style!css!sass'}
        ]
    },
    plugins: [
        new webpack.NoErrorsPlugin()
    ],
    devtool: "eval-source-map",
    devServer: {
        port: 8080,
        historyApiFallback: {
            index: '/'
        }
    },
    externals: {
        'Config': JSON.stringify({
            serverUrl: "http://pss/",
            authSuccessUrl: "http://localhost:8881/loginSuccess",
            podioClientId: "property-seller-solutions"
        })
    }
};

package.json file:

{
  "name": "pss",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --config ./webpack.config.js --hot --port 8080"
  },
  "author": "",
  "license": "BSD-2-Clause",
  "devDependencies": {
    "babel": "~6.5.2",
    "babel-core": "~6.9.1",
    "babel-loader": "~6.2.4",
    "babel-preset-es2015": "~6.9.0",
    "babel-preset-react": "~6.5.0",
    "babel-preset-stage-0": "~6.5.0",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.0",
    "gulp-html-replace": "^1.6.1",
    "gulp-react": "^3.1.0",
    "gulp-uglify": "^1.5.4",
    "history": "~3.0.0",
    "react": "~15.1.0",
    "react-dom": "~15.1.0",
    "react-hot-loader": "~1.3.0",
    "webpack": "~1.13.1",
    "webpack-dev-server": "~1.14.1"
  },
  "dependencies": {
    "chart.js": "^2.1.6",
    "connect-history-api-fallback": "~1.2.0",
    "halogen": "^0.2.0",
    "highcharts": "^4.2.5",
    "react-d3-basic": "^1.6.11",
    "react-infinite-scroll-component": "^1.4.1",
    "react-infinite-scroll-es2015": "^1.0.0"
  }
}

.htaccess File:

RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

在构建应用程序(webpack --hot - inline)之后,获得了两个输出文件 - index.js和index.html

我将两个文件和.htaccess文件复制到我的localhost根文件夹 .

索引路由正常工作..(http://localhost),但当我尝试重定向到http://localhost/home时显示"404 Not Found"错误(在此服务器上找不到请求的URL / home . )

当我使用webpack服务器运行应用程序时,这些路由工作正常(npm start) - http://localhost:8080/home

3 回答

  • 0

    工作 .

    使用以下.htaccess代码:

    RewriteEngine On  
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]
    
    RewriteRule ^ /index.html [L]
    

    并使用以下终端命令使用webpack构建项目:

    webpack --inline --history-api-fallback --progress -p
    
  • 3

    据我所知,如果你说http://localhost:8080/home它将进行服务器端路由器调用 . 你需要先设置它,

    https://github.com/reactjs/react-router/blob/master/docs/guides/ServerRendering.md

  • 0

    您需要将您的应用配置为在访问/ home时为index.html提供服务 .

相关问题