58 lines
2.0 KiB
JavaScript
58 lines
2.0 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { installDeps } from './common.js';
|
|
|
|
const SERVER_PORT = 3018;
|
|
|
|
async function main() {
|
|
installDeps();
|
|
|
|
let parentDirectory = process.cwd();
|
|
if (parentDirectory.includes('exam') && !fs.existsSync(path.join(parentDirectory, 'exam'))) {
|
|
parentDirectory = path.join(parentDirectory, '..');
|
|
}
|
|
|
|
const indexHtml = fs.readFileSync(path.join(parentDirectory, 'exam', 'index.html'), 'utf8');
|
|
const examTheory = fs.readFileSync(path.join(parentDirectory, 'exam', 'theory.json'), 'utf8');
|
|
let examTheoryData = JSON.parse(examTheory);
|
|
|
|
// import express and start the server
|
|
const express = await import('express');
|
|
const app = express.default();
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
app.get('/', (req, res) => {
|
|
res.setHeader('Content-Type', 'text/html');
|
|
res.setHeader('Cache-Control', 'no-cache');
|
|
res.setHeader('Pragma', 'no-cache');
|
|
res.setHeader('Expires', '0');
|
|
res.send(indexHtml);
|
|
});
|
|
|
|
app.get('/theory', (req, res) => {
|
|
res.setHeader('Content-Type', 'application/json');
|
|
res.json(examTheoryData);
|
|
});
|
|
|
|
app.post('/theory-answer', (req, res) => {
|
|
const questionId = req.body.questionId;
|
|
const answer = req.body.answer;
|
|
const index = examTheoryData.findIndex(q => q.id === questionId);
|
|
if (index !== -1) {
|
|
examTheoryData[index].answer = answer;
|
|
fs.writeFileSync(path.join(parentDirectory, 'exam', 'theory.json'), JSON.stringify(examTheoryData, null, 2));
|
|
res.status(200).json({ success: true });
|
|
} else {
|
|
res.status(404).json({ success: false, error: 'Question not found' });
|
|
}
|
|
});
|
|
|
|
const open = await import('open');
|
|
app.listen(SERVER_PORT, () => {
|
|
console.log(`Server is running on port ${SERVER_PORT}`);
|
|
open.default(`http://localhost:${SERVER_PORT}/`);
|
|
});
|
|
}
|
|
|
|
main(); |