新建文件后 输入命令 “code .” 打开文件所在的vscode 然后在命令行中需要输入的有 npm init -y → 会出来一个package.json文件 npm i express → 会出来一个node_modules npm i nodemon -g → 之后执行用命令 “nodemon 文件名” 即可 可以让我们在修改代码之后自动进行服务器的重启操作
express.js(自己根据老师的写的)
const fs
= require('fs');
const express
= require('express');
const app
= express();
app
.use(express
.urlencoded());
app
.use('/', express
.static('./public'));
app
.get('/api/v1/pets', (req
, res
) => {
const pets
= JSON.parse(fs
.readFileSync('./db/pets.json').toString());
res
.json(pets
);
});
app
.post('/api/v1/pets', (req
, res
) => {
const pets
= JSON.parse(fs
.readFileSync('./db/pets.json').toString());
pets
.push({
id
: Date
.now(),
...req
.body
});
fs
.writeFileSync('./db/pets.json', JSON.stringify(pets
));
res
.json({
code
: 1,
msg
: '保存数据成功',
});
});
app
.delete('/api/v1/pets/:id', (req
, res
) => {
const {
id
} = req
.params
;
const pets
= JSON.parse(fs
.readFileSync('./db/pets.json').toString());
const index
= pets
.findIndex(item
=> item
.id
== id
);
pets
.splice(index
, 1);
fs
.writeFileSync('./db/pets.json', JSON.stringify(pets
));
res
.json({
code
: 1,
msg
: '删除成功',
});
});
app
.put('/api/v1/pets/:id', (req
, res
) => {
const {
id
} = req
.params
;
const pets
= JSON.parse(fs
.readFileSync('./db/pets.json').toString());
const index
= pets
.findIndex(item
=> item
.id
== id
);
pets
[index
] = {
...{
id
},
...req
.body
};
fs
.writeFileSync('./db/pets.json', JSON.stringify(pets
));
res
.json({
code
: 1,
msg
: '修改成功',
});
});
app
.listen(3334, () => {
console
.log('服务器运行在3334端口');
})
main.js
const express
= require("express");
const app
= express();
const fs
= require("fs");
app
.use(express
.urlencoded());
app
.use("/", express
.static("./public"));
app
.get("/", (req
, res
) => {
res
.send("战斗吧,暴龙兽!");
});
app
.get("/api/v1/pets", (req
, res
) => {
const pets
= JSON.parse(fs
.readFileSync("./db/pets.json").toString());
res
.json(pets
);
});
app
.post("/api/v1/pets", (req
, res
) => {
const pets
= JSON.parse(fs
.readFileSync("./db/pets.json").toString());
pets
.push({
id
: Date
.now(),
...req
.body
});
fs
.writeFileSync("./db/pets.json", JSON.stringify(pets
));
res
.json({
code
: 1,
msg
: "保存数据成功",
});
});
app
.delete("/api/v1/pets/:id", (req
, res
) => {
const {
id
} = req
.params
;
const pets
= JSON.parse(fs
.readFileSync("./db/pets.json").toString());
const index
= pets
.findIndex((item
) => item
.id
== id
);
pets
.splice(index
, 1);
fs
.writeFileSync("./db/pets.json", JSON.stringify(pets
));
res
.json({
code
: 1,
msg
: "删除成功",
});
});
app
.put("/api/v1/pets/:id", (req
, res
) => {
const {
id
} = req
.params
;
const pets
= JSON.parse(fs
.readFileSync("./db/pets.json").toString());
const index
= pets
.findIndex((item
) => item
.id
== id
);
pets
[index
] = {
...{
id
},
...req
.body
};
fs
.writeFileSync("./db/pets.json", JSON.stringify(pets
));
res
.json({
code
: 1,
msg
: "修改成功",
});
});
app
.listen(3333, () => {
console
.log("服务器运行在3333端口");
});
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document
</title>
</head>
<body>
<h1>宠物列表
</h1>
<a href="/new.html">【新增】
</a>
<ul id="list"></ul>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$.ajax({
type: "GET",
url: "/api/v1/pets",
success: function (response) {
var strHtml = "";
response.forEach((item) => (strHtml += `<li>${item.name}</li>`));
$("#list").html(strHtml);
},
});
</script>
</body>
</html>
new.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>宠物新增
</title>
</head>
<body>
<ul>
<li><input type="text" id="name" placeholder="请输入宠物名字" /></li>
<li>
<input type="text" id="skills" placeholder="请输入宠物技能" />
</li>
<li><button id="btnSave">保存
</button></li>
</ul>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function () {
$("#btnSave").click(function () {
if ($("#name").val() && $("#skills").val()) {
$.post(
"/api/v1/pets",
{
name: $("#name").val(),
skills: $("#skills").val(),
},
function (data, textStatus, jqXHR) {
window.location.href = "/index.html";
}
);
} else {
alert("请输入内容");
}
});
});
</script>
</body>
</html>
转载请注明原文地址:https://ipadbbs.8miu.com/read-2435.html