diff --git a/app.js b/app.js index 70eeb5b..a646bda 100644 --- a/app.js +++ b/app.js @@ -38,16 +38,21 @@ const db = new sqlite3.Database(config.db_path, (err) => { } }) -const accessLogStream = fs.createWriteStream( - path.join(__dirname, 'log', 'access.log'), - { flags: 'a' } -) +if (config.PRODUCTION) { + let accessLogStream = fs.createWriteStream( + path.join(__dirname, 'log', 'access.log'), + { flags: 'a' } + ) + app.use(morgan('combined', { stream: accessLogStream })) +} else { + app.use(morgan('combined')) +} app.use(bodyParser.json()) -app.use(morgan('combined', { stream: accessLogStream })) app.use(session({ resave: false, saveUninitialized: false, + cookie: { maxAge: 3600000 }, secret: config.session_token })) @@ -84,12 +89,6 @@ try { // Serve the HTML file app.get('/', (req, res) => { - if (typeof req.session.cookie.expires === 'undefined') { - const hour = 3600000 - req.session.cookie.expires = new Date(Date.now() + hour) - req.session.cookie.maxAge = hour - } - res.sendFile(path.join(__dirname, 'index.html')) }) app.get('/asset/frontend.js', (req, res) => { @@ -97,9 +96,13 @@ app.get('/asset/frontend.js', (req, res) => { }) app.get('/newSession', (req, res) => { - log.info(req.session) - req.session.regenerate() - log.info(req.session) + log.info(req.session.id) + req.session.regenerate((error) => { + if (error) { + log.error(error) + } + }) + log.info(req.session.id) }) // Route to get a random animal name @@ -120,12 +123,10 @@ app.post('/recordButtonClick', (req, res) => { try { // const { buttonName, sessionId } = req.body; const result = req.body - log.error(result) - db.run( 'INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)', [ - result.session, + req.session.id, result.animal, result.button, result.time, diff --git a/asset/frontend.js b/asset/frontend.js index eb26ab5..bbb9550 100644 --- a/asset/frontend.js +++ b/asset/frontend.js @@ -13,18 +13,6 @@ async function getNextAnimal () { } } -// Function to set or retrieve the session ID cookie -function getSessionId () { - const sessionId = document.cookie.replace( - /(?:(?:^|.*;\s*)sessionId\s*=\s*([^;]*).*$)|^.*$/, - '$1' - ) - if (!sessionId) { - return newSession() - } - return sessionId -} - async function newSession () { setSessionStartTime() try { @@ -34,7 +22,6 @@ async function newSession () { } catch (error) { console.error('Error fetching data:', error) } - getSessionId() } // Function to set session start time @@ -52,7 +39,7 @@ function displayTimeDifference () { } // Function to record button clicks on the server -async function recordButtonClick (buttonName, sessionId) { +async function recordButtonClick (buttonName) { try { const currentTime = new Date() if (lastButtonClickTime) { @@ -62,7 +49,6 @@ async function recordButtonClick (buttonName, sessionId) { const bodyData = JSON.stringify({ animal, button: buttonName, - session: sessionId, difference: timeDifference, time: sessionStartTime }) @@ -86,11 +72,11 @@ async function recordButtonClick (buttonName, sessionId) { // Add click event listeners to the buttons document.getElementById('isCritterButton').addEventListener('click', () => { - recordButtonClick('is critter', getSessionId()) + recordButtonClick('is critter') }) document.getElementById('isNotCritterButton').addEventListener('click', () => { - recordButtonClick('is not critter', getSessionId()) + recordButtonClick('is not critter') }) document.getElementById('startOverButton').addEventListener('click', () => { diff --git a/config.js b/config.js index b3aa802..cccd097 100644 --- a/config.js +++ b/config.js @@ -1,5 +1,5 @@ const config = module.exports -const PRODUCTION = process.env.NODE_ENV === 'production' +config.PRODUCTION = process.env.NODE_ENV === 'production' const bole = require('bole') config.express = { @@ -7,7 +7,7 @@ config.express = { ip: '127.0.0.1' } -if (PRODUCTION) { +if (config.PRODUCTION) { config.express.ip = '0.0.0.0' config.db_path = 'db/results.db' config.session_token = process.env.SESSION_TOKEN