支持无后端纯静态
This commit is contained in:
parent
391a9fd7a7
commit
4c064765a7
1
assets/all.json
Normal file
1
assets/all.json
Normal file
File diff suppressed because one or more lines are too long
60
index.html
60
index.html
@ -63,7 +63,8 @@
|
|||||||
<div style="display: grid; place-items: center; color: white; height: 100%;">
|
<div style="display: grid; place-items: center; color: white; height: 100%;">
|
||||||
<div style="text-align: center;">
|
<div style="text-align: center;">
|
||||||
<!-- <h1 style="font-size: 45px;">答案之书</h1> -->
|
<!-- <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>
|
<div>
|
||||||
<div
|
<div
|
||||||
@ -84,25 +85,48 @@
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
function get() {
|
var xmlHttpRequest = new XMLHttpRequest();
|
||||||
var xmlHttpRequest = new XMLHttpRequest();
|
xmlHttpRequest.open("GET", "./assets/all.json"), true;
|
||||||
xmlHttpRequest.open("GET", "./api/get"), true;
|
xmlHttpRequest.setRequestHeader("Content-Type", "text/plain");
|
||||||
xmlHttpRequest.setRequestHeader("Content-Type", "text/plain");
|
xmlHttpRequest.send();
|
||||||
xmlHttpRequest.send();
|
xmlHttpRequest.onreadystatechange = function () {
|
||||||
|
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
|
||||||
xmlHttpRequest.onreadystatechange = function () {
|
var data = xmlHttpRequest.responseText;
|
||||||
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
|
var result = JSON.parse(data);
|
||||||
var data = xmlHttpRequest.responseText;
|
window.RESULT = result;
|
||||||
var result = JSON.parse(data);
|
console.log(result);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
15
index.js
15
index.js
@ -126,6 +126,21 @@ async function getOne() {
|
|||||||
// dbPool.close();
|
// 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) {
|
const server = createServer(async function (req, res) {
|
||||||
if (closeWithGrace.closing) {
|
if (closeWithGrace.closing) {
|
||||||
res.statusCode = 503
|
res.statusCode = 503
|
||||||
|
Loading…
Reference in New Issue
Block a user