感谢您的建议。
事实证明,我在考虑解决方案时非常seve。。我真正需要的是能够从客户端事件(加载组件)启动服务器端功能(从外部API获取资源)。如何提交表单,具有启动服务器端功能的动作。
在我的组件:
componentDidMount() {
const product_api_results = productApi.getProductItems()
console.log('product_api_results in CART Component: ', product_api_results)
/* now I have results and can put it in the Redux Store via action to reducer for other components to work with on client */
}
的productAPI.getProductItems(),该组件调用:
export function getProductItems() {
return axios.get('/api/get-products') // triggers a server side route
.then(function (response) {
console.log('client side response: ', response)
return response
})
}
在快递server.js文件时,服务器会看到这个URL,然后拿到正确的数据。 shopify.get()
来自shopify-node-api模块:
app.get('/api/get-products', function (req, res) { // '/api/get-products' called from client
shopify.get('/admin/products.json', function (err, data, headers) {
res.send(data)
})
})
您使用哪个库来进行API请求?的SuperAgent /取? – Deadfish
检出我目前正在构建的这个例子,已经支持universal react/redux应用程序:https://github.com/alexnm/react-seed。另外如果你想检查一个更复杂的例子,我建议MERN启动器:https://github.com/Hashnode/mern-starter –