fenyeqi-demo2
v1.0.0
Published
```JS const express = require("express"); const app = express(); const fs = require("fs"); app.listen("8080");
Downloads
3
Readme
const express = require("express");
const app = express();
const fs = require("fs");
app.listen("8080");
app.use(express.static("public"));
app.set("view engine", "ejs");
app.get("/", (req, res) => {
res.render("index")
})
app.get("/detail", (req, res) => {
let { id } = req.query;
let data = JSON.parse(fs.readFileSync("mock/list.json"))
let obj = data.find(item => +item.id === +id);
console.log(obj)
res.render("detail", obj)
})
app.get("/api/page", (req, res) => {
// 获取下标和每页个数
let { index, num } = req.query;
// 读取数据
fs.readFile("mock/list.json", (err, data) => {
// 判断是否成功
if (err) {
res.send({ code: 0 })
return;
}
// 转化JSON数据
data = JSON.parse(data);
// 返回数据
res.send({
code: 1,
arr: data.slice(index * num, (+index + 1) * num),
maxCode: Math.ceil(data.length / num)
})
})
})