简体   繁体   中英

How to display new question when user click button and previous question delete in node.js and pug?

I want to make simple quiz. When user click on button new question display to him. I don't have idea how to do it.

index.js

const express = require('express')
const app = express()

app.set('view engine', 'pug')
app.use(express.urlencoded({ extended: false }))

app.get('/', (req, res) => {
    questions = ['firstQuestion', 'secondQuestion']
    for (i = 0;i<questions.length;i++) {
        res.render('index', {question: question[i]})
    }
})

app.listen(3000)

index.pug

html(lang="en")
    head
        meta(charset="UTF-8")
        meta(http-equiv="X-UA-Compatible", content="IE=edge")
        meta(name="viewport", content="width=device-width, initial-scale=1.0")
        title Document
    body
        h1#a
        button(onclick='func()')
    script.
        function func() {
            document.getElementById("a").innerHTML = #{question}
        }

I am not sure how pug works but I have some general ideas for you

  1. You can save a dictionary in the server/database that maps ip to question number.
  2. You can push cookies to the user client and send the question index as header
  3. You can use live web sockets suchlike Socket.io

Keep in mind that you will have to save the data somewhere (userId to userQuestionIndex).. its depends of how secured you want the app to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM