1

I have 2 files jsx:

When I access localhost:8080 It's normal. But when I access localhost:8080/home It's show message in browser Cannot GET /home

How can I fix ?

Home.jsx:

import React from 'react';


class Home extends React.Component{

    constructor(props) {
        super(props);

    }

    render(){

        return(
            <div>
                <h1>Home ...</h1>
            </div>

        )
    }
}
export default Home;

index.jsx:

import React from 'react';
import {render} from 'react-dom';
import Home from './Home.jsx';
import { Router, Route, Link, browserHistory, IndexRoute } from 'react-router';



class App extends React.Component {

    render(){
        return(
            <div>
                <ul>
                     <li>Home</li>
                 </ul>
                 {this.props.children}
            </div>
        );
    }


}

render((
   <Router history = {browserHistory}>
      <Route path = "/" component = {App}>
         <IndexRoute component = {Home} />
         <Route path = "home" component = {Home} />

      </Route>
   </Router>

), document.getElementById('app'));

File webpack.config.js:

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');

var config = {
    entry: APP_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  devServer: {
      inline: true,
      port: 8080,
    contentBase: "./src/client",

    hot: true
   },
  module : {
    loaders : [
      {
        test : /\.jsx?/,
        include : APP_DIR,
        loader : 'babel-loader',
        query: {
               presets: ['es2015', 'react']
            }
      }
    ]
  }
};

module.exports = config;

2 Answers 2

2

In your devServer configuration, you need the historyApiFallback setting:

   devServer: {
      inline: true,
      port: 8080,
      contentBase: "./src/client",
      historyApiFallback: true,
      hot: true
   },

This tells the devServer to always return the root html page and let the client do the routing. In other words, to fallback to the root api route.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi David Tryon . I added but when i access localhost:8080/home It show in same result localhost:8080. It not switch to home page. Can you help me ?
At the moment you have the IndexRoute and the home route pointing to the same place. So, you will probably get the same result. Try to re-configure your Routes.
render(( <Router history = {browserHistory}> <Route path = "/" component = {App}> <IndexRoute component = {Home} /> <Route path = "home" component = {Home} /> </Route> </Router> ), document.getElementById('app')); It's not working :(
Oh yeah , it working fine with your way. Because a moment ago I'm not run terminal "npm run build" so not apply project. Thanks!
1

change <Route path = "home" component = {Home} /> to <Route path = "/home" component = {Home} />

1 Comment

It's still not working. Can you have another way ? :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.