react.js - 新手请教关于Express搭建React服务器的问题

查看:89
本文介绍了react.js - 新手请教关于Express搭建React服务器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

想在想要在服务器上搭建一个Node服务器server.js大致如下:

var app = express();
app.use(express.static(__dirname + '/'));
app.use(history());
app.get('/', function (req, res) {
    res.sendFile( __dirname + "/" + "index.html" );
})
app.listen(8081);

React使用React-Router做了路由,main.js大致如下:

reactDom.render(
    <Provider store={ store }>
        <Root/>
    </Provider>,
    document.getElementById("main")
)

Router大致如下:

const routesObj = (
    <Route path="/" name="App" component={ App }>
    <Route path='/home' name="Home" component={ Home }>
        <IndexRoute components={ Welcome }/>
        <Route path='/welcome' name="Welcome" component={ Welcome } />
    </Route>
    <IndexRedirect to='/home'/>
</Route>
);

export default class Root extends React.Component{
    render(){
        return <Router history={ browserHistory } routes={ routesObj }/>
    }
};

项目开发阶段一直使用的webpack-dev-server一切正常
-在浏览器直接localhost:8081访问可以访问到home
-在浏览器直接localhost:8081/home访问可以访问到home

但是以我的那个server.js启动的服务器只能localhost:8081/访问到home页面但是localhost:8081/home会找不到home,页面显示Cannot GET /home

求问大神们怎么解决,不太理解这块

解决方案

你通过 server.js启动的时候 是使用的express路由 也就是后端路由

而你dev-server的时候 是使用的react路由 也就是前端路由

二选其一

这篇关于react.js - 新手请教关于Express搭建React服务器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆