試圖爲客戶端應用程序創建服務器端API。客戶完全是在寫反應。在發展送達webdevserver在端口3000的服務器在端口3001 聽我添加代理到package.json
文件中的客戶端應用程序的如下:來自客戶端的代理請求通過create-react-app和webpack dev服務器將應用程序反饋給服務器
{
"name": "client",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-scripts": "0.8.5"
},
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-router": "^3.0.2",
"superagent": "^3.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:3001/"
}
但一旦我請求服務器API它失敗:
import Request from 'superagent';
export function searchTasks(text, callback) {
Request.get('http://localhost:3000/api/v1/tasks', response => {
callback(response.body.Search);
})
}
響應對象爲空。如果我嘗試使用3001端口請求API - 一切正常。看來web-dev-server沒有代理這些請求,或者,我錯過了一些額外的配置選項?
您是否嘗試執行'/ api/v1/tasks'而不是[docs](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/) template/README.md#proxying-api-requests-in-development)建議?另外,我注意到你正在使用'superagent'而不是'fetch' api - 切換到後者可能會有所幫助。 –
嘗試了不同的網址,但同樣的問題 –
如何將您的'proxy'更改爲'http:// localhost:3001'(丟失最後一個'/')? –