项目地址:git@github.com:longlongdan/Reactssr.gitnode
const Store = createStore(Rducer,window.info,applyMiddleware(thunk));
//浏览器端发送代码 至关于请求的是localhost:3000/api/news... return fetch('/api/news.json?secret=PP87ANTIPIRATE') .then((res)=>{ return res.json(); })
//后端proxy代理 const proxy = require('http-proxy-middleware'); app.use('/api',proxy({ target: 'http://47.95.113.63/ssr' }))
//客户端 const baseUrl = ''; const fetchClient = (url) => { return fetch(baseUrl+url); } export default fetchClient;
//服务端 import fetch from 'node-fetch' const baseUrl = 'http://47.95.113.63/ssr'; const fetchServer = (url) => { return fetch(baseUrl+url); } export default fetchServer;
//客户端 const Store = createStore(Rducer,window.info,applyMiddleware(thunk.withExtraArgument(fetchClient))); //服务端 const Store = createStore(Reducer, applyMiddleware(thunk.withExtraArgument(fetchServer)));
发送请求的代码,会接受第三个参数,就是咱们自定义的额外参数ios
export const getHomeList = (dispatch, getState, fetch) => { return fetch('/api/news.json?secret=PP87ANTIPIRATE') .then((res)=>{ return res.json(); }) }
//router.js export default [ { path: '/', component: Header, //共用的header头部 routes: [ { path: '/', exact: true, getData: Home.getData, component: Home, }, { path: '/Login', getData: ()=>{console.log('getData login')}, component: Login } ] } ]
在header里面须要继续渲染二级路由git
const Header = (props) => { return ( <div> <Link to='/'>Home</Link> <br/> <Link to='/Login'>Login</Link> { renderRoutes(props.route.routes) } </div> ) }