一个users用户的接口案例(增删改查)
-
目录结构展示
-
首先新建一个项目文件夹(demo)
-
终端:初始化(
npm init -y
) -
在
package.json
文件中配置脚本(配置热部署)-
热部署安装:
npm i nodemon
-
配置脚本
{ "name": "demo", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "nodemon ./src/main.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "nanoid": "^3.1.25", "nodemon": "^2.0.16" } }
-
-
详细代码展示
-
在
main.js
中const http = require("http"); const url = require("url"); const { handleUserModule } = require("./module/user"); // 查询用户 // 根据id删除用户 // 新增用户 // 根据id查询用户 // 根据id修改用户 const app = http.createServer((req, res) => { const { pathname } = url.parse(req.url); if ("/user" == pathname) { // 用户模块 handleUserModule(req, res); } }); app.listen(8888, () => { console.log("服务器开启成功"); });
-
在
user-data.js
中const users = [ { id: "1", username: "admin", nickname: "张三", age: 13, }, { id: "2", username: "lisi", nickname: "李四", age: 15, }, { id: "3", username: "wangwu", nickname: "王五", age: 20, }, { id: "4", username: "zhaoliu", nickname: "赵六", age: 9, }, ]; module.exports.users = users;
-
在
module
文件夹中的user.js
中const url = require("url"); const qs = require("querystring"); const { queryAll, queryOne, del, save, edit } = require("../dao/user"); const { readData } = require("../utils/utils"); const handleUserModule = async (res, req) => { const { pathname, query } = url.parse(res.url); let result = null; let body = null; switch (res.method) { case "GET": if (query) { const data = qs.parse(query); result = queryOne(data); } else { result = queryAll(); } break; case "POST": body = await readData(res); const length = save(body); if (length > 0) result = "添加成功"; result = length > 0 ? "添加成功" : "添加失败"; break; case "DELETE": body = await readData(res); const delUser = del(body); // console.log(delUser); result = delUser ? "删除成功" : "删除失败"; break; case "PATCH": body = await readData(res); edit(body); result = "修改成功"; break; } req.setHeader("content-type", "application/json"); req.end(JSON.stringify(result)); }; module.exports.handleUserModule = handleUserModule;
-
在
dao
文件夹中的user.js
中// npm i nanoid const { nanoid } = require("nanoid"); const { users } = require("../user-data"); /** * 查询全部 */ function queryAll() { return users; } /** * 根据id查询 */ function queryOne(data) { for (const item of users) { if (data.id == item.id) { return item; } } } /** * 保存 */ function save(body) { return users.push({ ...body, id: nanoid() }); // console.log(users); } /** * 删除 */ function del(body) { let delUser = null; users.forEach((item, index) => { if (body.id == item.id) { delUser = users.splice(index, 1); } }); return delUser; } /** * 修改 */ function edit(body) { users.forEach((item, index) => { if (body.id === item.id) { for (const key in body) { item[key] = body[key]; } } }); } module.exports = { queryAll, queryOne, edit, save, del, };
-
在
utils
文件夹中的utils.js
中/** * 读取body中的数据 * @param res * @returns {undefined} */ function readData(res) { return new Promise((resolve, reject) => { res.on("data", (data) => { resolve(JSON.parse(data.toString())); }); }); } exports.readData = readData;
-
这里的使用的接口测试工具是
Postman
-
查询全部(
GET
) -
查询单个(
GET
根据id
查询) -
添加(POST
id
是通过安装第三方模块nanoid
自动生成的) -
删除(
DELETE
根据id
进行删除) -
修改(
PATCH
根据id
进行修改) -
每一次添加、删除、修改都可以返回重新通过
GET
请求查询全部验证(省略)
-