在react + react-router + express + webpack中刷新页面时找不到URL

时间:2017-09-04 10:14:04

标签: reactjs express webpack-dev-server react-router-v4 webpack-3

我是新的反应路由器,快递和其他东西。我一直在关注如何预防"无法获取网址"在反应应用程序中刷新页面时。

我发现了很多关于这个问题的相关Q& A主题并尝试了。但是,不幸的是......它失败了。也许是因为webpack的版本已经升级到{i}我现在正在使用的版本。

我尝试使用v3.5.5并将historyApiFallback属性放在webpack配置文件的devServer对象中。这也是失败的。

所以,我会在下面提供一些配置文件。请检查并告诉我,我做错了

1)webpack.config.js

webpack-dev-server

2)index.js / server file

const path = require('path');

module.exports = {
  entry: './client/src/index.jsx',
  output: {
    path: path.resolve(__dirname, '/client/dist/js'),
    filename: 'app.js',
    publicPath: './client/dist/js'
  },
  devServer: {
    historyApiFallback: {
      index: path.resolve(__dirname, './server/static/index.html'),
    }
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        include: path.join(__dirname, '/client/src'),
        use: 'babel-loader',
        query: {
          presets: ["react", "es2015"],
          plugins: ["transform-class-properties"]
        }
      },
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader'
      }
    ]
  }
};

3)package.json

const express = require('express');
const bodyParser = require('body-parser');
const passport = require('passport');
const config = require('./config');

// connect to the database and load models
require('./server/models').connect(config.dbUri);

const app = express();

// tell the app to look for static files in these directories
app.use(express.static('./server/static/'));
app.use(express.static('./client/dist/'));

// tell the app to parse HTTP body messages
app.use(bodyParser.urlencoded({ extended: false }));

// pass the passport middleware
app.use(passport.initialize());

// load passport strategies
const localSignupStrategy = require('./server/passport/local-signup');
const localLoginStrategy = require('./server/passport/local-login');
passport.use('local-signup', localSignupStrategy);
passport.use('local-login', localLoginStrategy);

// pass the authenticaion checker middleware
const authCheckMiddleware = require('./server/middleware/auth-check');
app.use('/api', authCheckMiddleware);

// routes
const authRoutes = require('./server/routes/auth');
const apiRoutes = require('./server/routes/api');
app.use('/auth', authRoutes);
app.use('/api', apiRoutes);

// start the server
app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000');
});

0 个答案:

没有答案