Can't found URL when refresh page in react + react-router + express + webpack

242 views Asked by At

I'm new to react-router, express and other stuff. I've been following some tutorial out there about how to prevent "Cannot get URL" when refresh a page in react app.

I found so many related Q&A topics about this issue and try it. But, unfortunately .. it was failed. Maybe because the version of the webpack have been upgrade to v3.5.5 that i'm using now.

I tried using webpack-dev-server and put historyApiFallback property in devServer object on webpack configuration file. It was a failed too.

So, i would provide some configuration files below. Please check and tell me what i'm doing wrong or should i do.

1) webpack.config.js

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'
      }
    ]
  }
};

2) index.js / server file

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');
});

3) package.json

{
  "name": "example-app",
  "version": "1.0.0",
  "description": "Example App",
  "main": "index.js",
  "scripts": {
    "start": "nodemon --use_strict index.js",
    "bundle": "webpack"
  },
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^1.0.2",
    "body-parser": "^1.17.2",
    "bootstrap": "^4.0.0-alpha.6",
    "express": "^4.15.4",
    "jsonwebtoken": "^7.4.3",
    "material-ui": "^0.19.0",
    "mongoose": "^4.11.7",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0",
    "react": "^15.6.1",
    "react-addons-css-transition-group": "^15.6.0",
    "react-addons-transition-group": "^15.6.0",
    "react-dom": "^15.6.1",
    "react-router-dom": "^4.2.2",
    "react-tap-event-plugin": "^2.0.1",
    "reactstrap": "^4.8.0",
    "validator": "^8.0.0"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "clean-webpack-plugin": "^0.1.16",
    "css-loader": "^0.28.5",
    "file-loader": "^0.11.2",
    "html-webpack-plugin": "^2.30.1",
    "nodemon": "^1.11.0",
    "style-loader": "^0.18.2",
    "url-loader": "^0.5.9",
    "webpack": "^3.5.5",
    "webpack-dev-server": "^2.7.1"
  }
}
0

There are 0 answers