39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
var express = require("express");
|
|
var router = express.Router();
|
|
const db = require("../models");
|
|
const path = require("path");
|
|
|
|
/* GET home page. */
|
|
router.get("/", function (req, res, next) {
|
|
res.render("index", { title: "Express" });
|
|
});
|
|
|
|
// API endpoint to get quiz and questions
|
|
router.get("/api/quiz", async function (req, res) {
|
|
try {
|
|
const quiz = await db.quiz.findOne({
|
|
include: [{ model: db.question, as: "questions" }],
|
|
});
|
|
if (!quiz) {
|
|
return res.json({ success: false, error: "Quiz not found" });
|
|
}
|
|
// Parse options from JSON string to array for each question
|
|
const quizData = quiz.toJSON();
|
|
quizData.questions = quizData.questions.map((q) => ({
|
|
...q,
|
|
options:
|
|
typeof q.options === "string" ? JSON.parse(q.options) : q.options,
|
|
}));
|
|
res.json({ success: true, data: quizData });
|
|
} catch (err) {
|
|
res.json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
// Serve quiz.html at /quiz
|
|
router.get("/quiz", function (req, res) {
|
|
res.sendFile(path.join(__dirname, "../quiz.html"));
|
|
});
|
|
|
|
module.exports = router;
|