【问题标题】:How to set Cookie in Electron React using npm react-cookie package?如何使用 npm react-cookie 包在 Electron React 中设置 Cookie?
【发布时间】:2023-02-02 22:08:34
【问题描述】:

我正在尝试使用 npm react-cookie 包在 electron-react 应用程序中设置 cookie

并像那样设置cookie

import { useCookies } from "react-cookie";
const [cookies, setCookie, removeCookie] = useCookies();
setCookie("isWorking", 121, { path: "/" });

它在开发模式下工作正常但是当我构建电子应用程序时它没有设置我的 cookie 在构建应用程序中

开发中的 cookie 快照

生产/电子构建应用程序中的快照

【问题讨论】:

    标签: javascript reactjs cookies electron react-cookie


    【解决方案1】:

    在生产中,电子模式在文件系统中运行您的反应应用程序,因此 cookie 永远不会保存在文件系统中为了保存 cookie,您必须在服务模式下运行电子反应应用程序

      const server = require("./server");
    
        const createServer = async () => {
          if (isDev) {
            createWindow();
            win.loadURL("http://localhost:3000");
          } else {
            let port = null;
            for (let i = 5001; i < 6000; i++) {
              let isFree = await isPortFree(i);
          
              if (isFree) {
                port = i;
                break;
              }
            }
            currentPort = port;
            server.listen(port, () => {
              console.log(`Example app listening at http://localhost:${port}`);
              createWindow();
              win.loadURL(`http://localhost:${currentPort}`);
            });
          }
        };
    app.whenReady().then(createServer);
    

    你的 server.js 文件将是这样的

    const path = require("path");
    const express = require("express");
    
    const app = express();
    
    app.use(express.static(path.join(__dirname, "/app")));
    
    app.get("/", (req, res) => {
      res.sendFile(path.join(__dirname, "/app/index.html"));
    });
    
    module.exports = app;
    

    【讨论】:

      猜你喜欢
      • 2018-09-15
      • 2018-05-30
      • 2018-03-13
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多