項目地址: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> ) }