//this is controller
exports.currentUser = (req, res) => {
res.send(req.user);
};
//this is the route
const authCtrl = require("../controllers/authCtrl");
const router = require("express").Router();
router.get("/current_user", authCtrl.currentUser);
这就是我为express设置路由器的方法
app.use("/auth", require("./routes/authRoutes"));
以下是身份验证还原程序:
import { FETCH_USER } from "../actions/types";
const INITIAL_STATE = { _id: null, googleId: null, displayName: "" };
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case FETCH_USER:
return action.payload;
default:
return state;
}
};
import axios from "axios";
const axiosInstance = axios.create({
baseURL: "http://localhost:3001",
timeout: 3000,
});
import { FETCH_USER, FETCH_BLOGS, FETCH_BLOG } from "./types";
export const fetchUser = () => async (dispatch, getState, api) => {
try {
const res = await api.get("/auth/current_user");
dispatch({ type: FETCH_USER, payload: res.data });
} catch (e) {
console.log(e.message);
}
};
这是客户端商店:
import thunk from "redux-thunk";
import axios from "axios";
const axiosInstance = axios.create({
baseURL: "http://localhost:3001",
});
//we are passing axiosInstance to all action creators
const store = createStore(
reducers,
window.INITIAL_STATE,
applyMiddleware(thunk.withExtraArgument(axiosInstance))
);
到现在为止,一直都还不错。但问题是创建服务器端存储:我这样做了,但没有工作:
import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import reducers from "../client/reducers";
export default (req) => {
const store = createStore(
reducers,
{},
applyMiddleware(thunk)
);
console.log("store from servre", store);
return store;
};