最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

React 的 Express

网站源码admin34浏览0评论

React 的 Express

React 的 Express

我想创建一个反应应用程序,登录后在登录系统中我将电子邮件保存到会话中。但是在下一个请求中,保存的数据在这里变为空是我的代码,

快递专区

  app.use(cors({
  
    origin: "http://localhost:3000",
    credentials:true,       
    methods: ['POST', 'PUT', 'GET', 'OPTIONS', 'HEAD'],
    optionSuccessStatus:200,
}))
  // Router setup middlewares
  app.options('http://localhost:3000', cors()) 

快速登录部分

router.post("/login", async (req, res) => {
  superAdminHelpers
    .Login(req.body.body)
    .then((response) => {
      req.session.superAdmin = response.email;
      res.send(response);
   
    })
    .catch((err) => {
      res.status(401).send(err);
    });
});

正确获取回复邮件

响应请求

   axios
    .post(LoginURL, {
      body: loginDetails,
      headers: {
        'Content-Type': 'application/json'
      },
      withCredentials: true
      
    })
回答如下:

我有同样的问题。发生的事情是,每次我在登录后使用 axios 调用其他 api 时,每个新请求都会创建一个新会话。我解决的方法是:

前端:

import axios from 'axios'

const request = axios.create({
  baseURL:   process.env.NEXT_PUBLIC_API_BACKENDURL,
  timeout: 20000,
  withCredentials: true,
});




export default request

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论