2021年1月2日星期六

express-session Error: Can't send headers after they are sent

I am using express-session to prevent variable from being global for more than one client, so they don't get tangled up. Here is my entire express code:

const path = require("path");  const bodyParser = require("body-parser");  const express = require("express");  const app = express();  const session = require('express-session');    app.use(session({    secret: process.env.SECRET,    name: 'lastQuestion',    cookie: {      sameSite: "lax",      secure: true,      httpOnly: true    }  }));    app.use(bodyParser.urlencoded({ extended: false }));  app.set('views', __dirname + '/public');  app.engine('html', require('ejs').renderFile);  app.set('view engine', 'html');    app.get('/', (req, res) => {    session.lastQuestion = generateQuestion();      res.render(__dirname + '/public/index.html', {response: "Hello!", question: session.lastQuestion, instructions: true});  });    app.listen(process.env.PORT);    app.post("/prompt", (req, res) => {      console.log("New prompt: " + req.body.inputOne);    session.lastQuestion = generateQuestion();      res.render(__dirname + '/public/index.html', {response: evaluatePrompt(req.body.inputOne.toLowerCase()), question: session.lastQuestion, instructions: false});  });    app.post("/answer", (req, res) => {      console.log("New answer: " + req.body.inputTwo);    console.log(req.body.question + session.lastQuestion);    analyzeConversationAndWriteToLTM(session.lastQuestion, req.body.inputTwo);    session.lastQuestion = generateQuestion();      res.render(__dirname + '/public/index.html', {response: "Hello!", question: session.lastQuestion, instructions: false});  });    app.post("/regenerate", (req, res) => {    session.lastQuestion = generateQuestion();      res.render(__dirname + '/public/index.html', {response: "Hello!", question: session.lastQuestion, instructions: false});  });  

/answer is the one that remembers the session.lastQuestion cookie. Anyways, even though I'm using cookies, session.lastQuestion is updated for every client when one client updates it, resulting in desyncing.

https://stackoverflow.com/questions/65544373/express-session-error-cant-send-headers-after-they-are-sent January 03, 2021 at 05:39AM

没有评论:

发表评论