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

时间:2016-06-30 12:45:19

标签: .htaccess reactjs webpack webpack-dev-server

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

当我使用webpack服务器(npm start)

运行应用程序时,路由工作正常

webpack.config文件

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文件:

{
  "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文件:

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 :(得分:3)

的工作。

使用以下.htaccess代码:

public interface Document {
    void writeTo(ServletResponse response);

    Document convert(Charset targetCharset);
}

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

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

RewriteRule ^ /index.html [L]

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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