赞
踩
npm i then-fs
- // 需求: es6模块化,用async、await和then-fs模块实现操作文件数据
- // 功能: 增删改查;
- // 导入对应模块
- import thenFs from 'then-fs';
- // ES6中不支持 __dirname ,所以当前这个案例还是使用相对路径;
- const url = './06.json';
-
- // 1.查询; - await必须出现在async修饰的函数中;
- export async function getData() {
- try {
- // 利用 thenFs 读取文件内容
- const str = await thenFs.readFile(url, 'utf-8'); // 返回的是Promise
- // 把str转换为数组
- const arr = JSON.parse(str);
- // 返回数组, 将来别人调用这个函数,就可以获取数组了
- return arr;
- } catch (e) {
- return '获取图书列表失败';
- }
- }
- // // 测试获取
- // getData().then(res => console.log(res));
-
- // 2.添加; - await必须出现在async修饰的函数中;
- export async function addData(obj) {
- // 先读取文件中的数据
- const arr = await getData();
- // 然后向数组中添加元素
- obj.id = arr[arr.length - 1].id * 1 + 1;
- arr.push(obj);
- // 写入数据
- try {
- // 代码 thenFs.writeFile()之前最好写await,有阻塞功能;
- await thenFs.writeFile(url, JSON.stringify(arr));
- return '添加成功';
- } catch (e) {
- return '添加失败';
- }
- }
- // // 测试添加
- // addData({
- // bookname: '三体', author: '刘慈欣', publisher: '武汉人民出版社'
- // }).then(res => console.log(res));
-
- // 3.删除; - await必须出现在async修饰的函数中;
- export async function delData(id) {
- // 先读取文件中的数据
- const arr = await getData();
- // 过滤模拟删除功能
- const newArr = arr.filter(item => item.id != id);
- // 写入文件
- try {
- // 代码 thenFs.writeFile()之前最好写await,有阻塞功能;
- await thenFs.writeFile(url, JSON.stringify(newArr));
- return '删除成功';
- } catch (e) {
- return '删除失败';
- }
- }
- // // 测试删除
- // delData(5).then(res => console.log(res));
-
- // 4.修改;
- export async function putData(obj) {
- // 先读取文件中的数据
- const arr = await getData();
- // 修改 - 先删除,在添加
- const index = arr.findIndex(item => item.id == obj.id);
- arr.splice(index, 1, obj);
- // 写入数据
- try {
- // 代码 thenFs.writeFile()之前最好写await,有阻塞功能;
- await thenFs.writeFile(url, JSON.stringify(arr));
- return '修改成功';
- } catch (e) {
- return '修改失败';
- }
- }
- // // 测试
- // putData({
- // id: 1, bookname: '三国演义-后传',author: '罗贯东',publisher: '传智人民出版社'
- // }).then(res => console.log(res))
-
- // 默认导出
- export default { getData, addData, delData, putData }

npm i express
import book from './obtain.js';
npm i cors
- import express from 'express';
- import btain from './obtain.js';
-
- const app = express()
-
- app.get('/aaa/bbb', async (req, res) => {
- const arr = await btain.getData()
- // 支持浏览器ajax跨域访问, * 代表任意人都可以来请求
- res.setHeader('Access-Control-Allow-Origin', '*')
- if (arr instanceof Array) {
- res.send({ code: 200, msg: '获取图书列表成功', data: arr })
- } else {
- res.send({ code: 501, msg: '获取图书列表失败' })
- }
- // res.send('获取图书列表成功')
-
- })
- // 启动 web 服务器
- app.listen(8080, () => {
- console.log(' http://127.0.0.1:8080')
- })
-

- import express from 'express';
-
- import book from './obtain.js';
- const app = express()
-
- app.use(express.urlencoded({ extended: true }))
- app.use(express.json())
-
- //设置全局跨域
- const cors = require('cors')
- app.use(cors())
- //post
- app.post('/ccc/ddd', async (req, res) => {
-
- const arr = await book.addData(req.body)
- // console.log(arr);
- if (arr == '添加成功') {
- res.send({ code: 201, msg: '添加图书成功' })
- } else {
- res.send({ code: 501, msg: '添加图书失败' })
- }
- // res.send(`添加图书成功`)
- })
-
-
-
- //启动服务器
- app.listen(5521, () => {
- console.log(`http://127.0.0.1:5521`);
- })

- import express from 'express';
-
- import book from './obtain.js';
-
-
-
- const app = express()
-
- app.use(express.urlencoded({ extended: true }))
- app.use(express.json())
-
- //设置全局跨域
- const cors = require('cors')
- app.use(cors())
-
- //post DELETE
- app.delete('/eee/fff', async (req, res) => {
- res.set('Access-Control-Allow-Origin', "*"); // 设置响应头
- const arr = await book.delData(req.query.id)
- console.log(arr);
- if (arr == '删除成功') {
- res.send({ code: 201, msg: '删除图书成功' })
-
- } else {
- res.send({ code: 501, msg: '删除图书失败' })
- }
-
-
- // res.send(`添加图书成功`)
- })
-
-
- //启动服务器
- app.listen(7761, () => {
- console.log(`http://127.0.0.1:7761`);
- })

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。