簡體   English   中英

快遞 session 在部署到 heroku 和 netlify 后不持久

[英]express session not persisting after deploying to heroku and netlify

我將我的 React 前端部署到 netlify,並將后端表達到 Heroku,我正在使用 session。

session 在本地完美運行,但當我部署它時它不起作用

當我檢查數據庫時,session 已經創建,但是當我console.log(req.session)它沒有記錄任何東西

所以,看起來 session 保存在數據庫上,但它沒有保存在 heroku 服務器上

我需要在 heroku 上配置會話嗎?


mongoose.connect(
  "mydburl",
  {
    useNewUrlParser: true,
  }
);

const store = new MongoDBStore({
  uri:
    "mydburl",
  collection: "sessions",
});

app.use(
  cors({
    domain: url,
    origin: [url],
    methods: ["GET", "POST", "DELETE"],
    credentials: true,
  })
);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.set("trust proxy", 1);
app.use(
  session({
    key: "userID",
    secret: "subscribe",
    proxy: true,
    resave: false,
    saveUninitialized: false,
    store: store,
    cookie: {
      secure: false,
      httpOnly: false,
      maxAge: null, //1000 * 60 * 60 * 24 * 7, 1 week
      domain: url,
    },
  })
);

app.get("/login", (req, res) => {
  if (req.session.user) {
    console.log(req.session.user);
    res.send({ isLogin: true, user: req.session.user });
  } else {
    console.log("req.session.user doesn't exist");
    res.send("no");
  }
});

app.post("/login", (req, res) => {
  const { username, password } = req.body;

  RegisterModel.find({ username }, (err, result) => {
    if (err) {
      console.log(err);
    }
    if (result.length > 0) {
      bcrypt.compare(password, result[0].password, (error, response) => {
        if (response) {
          req.session.user = result;
          res.send(result);
        } else {
          res.send("비밀번호가 맞지 않습니다");
        }
      });
    } else {
      res.send("아이디가 맞지 않습니다");
    }
  });
});

如果您還可以在創建 session 的位置添加前端部分,那將非常有幫助。 但是我認為將前端和后端結合起來應該可以幫助您解決這個問題。

在 Heroku 上部署前端和后端。 將您的index.jsserver.js更改為服務器的入口點,如下所示:

...
if (process.env.NODE_ENV === "production") {
  //Set static folder
  app.use(express.static("client/build"));
  app.get("*", (req, res) => {
    res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
  });
}

const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server running on port ${port}`));

module.exports = { app };

在這里,我假設您的前端部分位於客戶端文件夾中。 您可以相應地更改它。

root_folder
   -server.js
   ....rest of server files
   package.json
   -client
        -build
        ....rest of React part
        package.json

此外,將此添加到服務器的 package.json 文件中。

"scripts": {
    ....
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  },

上述命令將首先安裝客戶端依賴項,然后創建用於部署的構建。

Now, if check Heroku URL is https://xyz.herokuapp.com and it should display your front-end and the rest will work the same. 在本地,您可以通過手動創建構建(npm run build)並將process.env.NODE_ENV === "production"更改為process.env.NODE_ENV === "development"來檢查這一點,並相應地更改build文件夾路徑。 現在,檢查http://localhost:5000或您的本地后端服務器 URL。

你可以在這里閱讀更多信息: https://create-react-app.dev/docs/deployment/#heroku

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM