首页 > 解决方案 > node.js 和 pug 中用户单击按钮和删除前一个问题时如何显示新问题?

问题描述

我想做一个简单的测验。当用户单击按钮时,向他显示新问题。我不知道该怎么做。

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)

指数.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}
        }

标签: javascriptnode.jsexpresswebpug

解决方案


我不确定哈巴狗是如何工作的,但我有一些一般的想法给你

  1. 您可以在将 ip 映射到问题编号的服务器/数据库中保存字典。
  2. 您可以将 cookie 推送到用户客户端并将问题索引作为标题发送
  3. 您可以使用实时网络套接字,例如 Socket.io

请记住,您必须将数据保存在某处(userId 到 userQuestionIndex).. 这取决于您希望应用程序的安全程度。


推荐阅读