1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee

支持无后端纯静态

This commit is contained in:
程序员小墨 2022-09-26 00:24:08 +08:00
parent 391a9fd7a7
commit 4c064765a7
3 changed files with 58 additions and 18 deletions

1
assets/all.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -63,7 +63,8 @@
<div style="display: grid; place-items: center; color: white; height: 100%;">
<div style="text-align: center;">
<!-- <h1 style="font-size: 45px;">答案之书</h1> -->
<img class="do-not-select" src="./assets/img/title.png" alt="答案之书" style="max-width: 72%; max-height: 80px;">
<img class="do-not-select" src="./assets/img/title.png" alt="答案之书"
style="max-width: 72%; max-height: 80px;">
</div>
<div>
<div
@ -84,25 +85,48 @@
</p>
</div>
<script>
function get() {
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("GET", "./api/get"), true;
xmlHttpRequest.setRequestHeader("Content-Type", "text/plain");
xmlHttpRequest.send();
xmlHttpRequest.onreadystatechange = function () {
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
var data = xmlHttpRequest.responseText;
var result = JSON.parse(data);
console.log(result);
var chinese = result.data["chinese"];
var english = result.data["english"];
console.log("chinese", chinese, "english", english);
document.getElementById("chinese").innerHTML = chinese;
document.getElementById("english").innerHTML = english;
}
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("GET", "./assets/all.json"), true;
xmlHttpRequest.setRequestHeader("Content-Type", "text/plain");
xmlHttpRequest.send();
xmlHttpRequest.onreadystatechange = function () {
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
var data = xmlHttpRequest.responseText;
var result = JSON.parse(data);
window.RESULT = result;
console.log(result);
}
}
function get() {
var one = window.RESULT[Math.floor(Math.random() * window.RESULT.length)];
console.log(one);
var chinese = one["chinese"];
var english = one["english"];
console.log("chinese", chinese, "english", english);
document.getElementById("chinese").innerHTML = chinese;
document.getElementById("english").innerHTML = english;
}
// function get() {
// var xmlHttpRequest = new XMLHttpRequest();
// xmlHttpRequest.open("GET", "./api/get"), true;
// xmlHttpRequest.setRequestHeader("Content-Type", "text/plain");
// xmlHttpRequest.send();
// xmlHttpRequest.onreadystatechange = function () {
// if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
// var data = xmlHttpRequest.responseText;
// var result = JSON.parse(data);
// console.log(result);
// var chinese = result.data["chinese"];
// var english = result.data["english"];
// console.log("chinese", chinese, "english", english);
// document.getElementById("chinese").innerHTML = chinese;
// document.getElementById("english").innerHTML = english;
// }
// }
// }
</script>
</body>

View File

@ -126,6 +126,21 @@ async function getOne() {
// dbPool.close();
}
// 取得全部
async function getAll() {
let sql = `SELECT * FROM answer ORDER BY id;`;
try {
let result = await dbPool.query(sql);
console.log(JSON.stringify(result));
fs.writeFileSync("all.json", JSON.stringify(result));
return result;
} catch (err) {
console.error(err);
}
// dbPool.close();
}
getAll();
const server = createServer(async function (req, res) {
if (closeWithGrace.closing) {
res.statusCode = 503