現在、2 つのサーバーを実行しています。
- express で構築した REST API からデータを取得する react を使用してビューを提供します。
- ビューに REST API を提供します。
以下は、ユーザーをログインさせるための私のアクションです。
// Redux Action
export function loginUser(creds, role) {
return dispatch => {
// We dispatch requestLogin to kickoff the call to the API
dispatch(requestLogin(creds));
return axios.post(`${ROOT_URL}/login/${role}`, creds).then((response) => {
console.log(response);
if(response.status === 200) {
// If login was successful, set the token in local storage
localStorage.setItem('id_token', response.data);
// Dispatch the success action
dispatch(receiveLogin(response));
return response;
}
}).catch(err => {
// If there was a problem, we want to
// dispatch the error condition
dispatch(loginError(err.data));
return err;
});
};
}
エラーをキャッチして何が起こるかを確認するために、意図的にデータベースを切断しました。だから、これは私がターミナルで見ることができるものです:
12:49:24 Project-0 Server is listening at port 3000
12:49:24 Project-0 Mongoose disconnected
12:49:24 Project-0 Mongoose connection error: MongoError: connect ECONNREFUSED 192.168.1.116:27017
12:49:34 Project-0 Wed, 13 Apr 2016 07:19:34 GMT express deprecated res.send(status): Use res.sendStatus(status) instead at app/index.js:61:7
12:49:34 Project-0 OPTIONS /login/admin Wed, 13 Apr 2016 07:19:34 GMT ::ffff:192.168.1.134 200 5.894
12:49:35 Project-0 POST /login/admin Wed, 13 Apr 2016 07:19:35 GMT ::ffff:192.168.1.134 - -
これで、ログイン フォームを送信すると、ステータスが pending から Cancelled に変わります。
axios を使用してこのステータスを取得するにはどうすればよいでしょうか。それとも、Express 自体でこのためのメカニズムを作成する必要がありますか?
注:タグが存在せず、新しいタグを作成できないため、axio にタグを付けることができませんでした。