NodeJS--- fs读写本地文件详细指南

本文主要讲fs读写本地文件详细指南。

一 示例代码目录:

 

二 具体读写方法: 

2.1 data-utils.js

const fs = require('fs');
const path = require('path')

const dataUtils = {};

// 读取json文件内容
dataUtils.readFile = function(fileName) {
const promise = new Promise(function (resolve, reject) {
fs.readFile(path.join(__dirname, fileName), 'utf8', (err, data) => {
if (err) {
reject(err);
return;
}

resolve(JSON.parse(data));
});
});
return promise;
};

// 写入json文件内容
dataUtils.writeFile = function(fileName, data) {
const promise = new Promise(function (resolve, reject) {
let newArr = JSON.stringify(data, null,"\t"); // 将数组转成json格式
fs.writeFile(path.join(__dirname, fileName), newArr, 'utf8', (err) => {
if (err === null) {
resolve('保存成功');
} else {
reject('保存失败');
}
});
});
return promise;
};

module.exports = dataUtils;

2.2 shangji-lists.json

[
{
"id": 1676360091561,
"source": "百度",
"time": "2023-02-14T07:34:51.551Z",
"province": "内蒙古",
"contactInformation": "电话:15332472483",
"type": "经销",
"regionalManager": "小",
"companyName": "华康医药公司",
"registeredCapital": "3000万",
"customerContactPerson": "张三",
"jobTitle": "后勤主管",
"customerLevel": "A",
"salesType": "代理",
"intendedproduct": "成品",
"status": "已成交",
"dealMoney": "500万",
"followUpDate": "2023.1.5",
"followUpInformation": [
{
"time": "2023.1.5",
"contents": "客户已付定金"
},
{
"time": "2022.12.5",
"contents": "客户已签合同"
},
{
"time": "2022.11.24",
"contents": "已经和客户确定数量和价格"
},
{
"time": "2023.11.22",
"contents": "初次交流,客户对产品非常满意。待客户回去和公司主管确定购买数量和意向价格"
}
]
},
{
"id": 1676360091571,
"source": "阿里",
"time": "2023-02-14T07:34:51.551Z",
"province": "台湾省",
"contactInformation": "电话:15332472483",
"type": "零售",
"regionalManager": "彭",
"companyName": "华康医药公司",
"registeredCapital": "3000万",
"customerContactPerson": "张三",
"jobTitle": "后勤主管",
"customerLevel": "A",
"salesType": "经销商",
"intendedproduct": "成品",
"status": "不跟进",
"dealMoney": "500万",
"followUpDate": "2023.1.5",
"followUpInformation": [
{
"time": "2023.1.5",
"contents": "客户已付定金"
},
{
"time": "2022.12.5",
"contents": "客户已签合同"
},
{
"time": "2022.11.24",
"contents": "已经和客户确定数量和价格"
},
{
"time": "2023.11.22",
"contents": "初次交流,客户对产品非常满意。待客户回去和公司主管确定购买数量和意向价格"
}
]
},
{
"id": 1676360091581,
"source": "阿里",
"time": "2023-02-14T07:34:51.551Z",
"province": "浙江省",
"contactInformation": "电话:15332472483",
"type": "零售",
"regionalManager": "玉",
"companyName": "华康医药公司",
"registeredCapital": "3000万",
"customerContactPerson": "张三",
"jobTitle": "后勤主管",
"customerLevel": "B",
"salesType": "代理",
"intendedproduct": "贴牌",
"status": "已成交",
"dealMoney": "500万",
"followUpDate": "2023.1.5",
"followUpInformation": [
{
"time": "2023.1.5",
"contents": "客户已付定金"
},
{
"time": "2022.12.5",
"contents": "客户已签合同"
},
{
"time": "2022.11.24",
"contents": "已经和客户确定数量和价格"
},
{
"time": "2023.11.22",
"contents": "初次交流,客户对产品非常满意。待客户回去和公司主管确定购买数量和意向价格"
}
]
},
{
"id": 1676360091511,
"source": "百度222",
"time": "2023-02-14T07:34:51.551Z",
"province": "重庆市",
"contactInformation": "电话:15332472483",
"type": "经销",
"regionalManager": "彭",
"companyName": "华康医药公司",
"registeredCapital": "3000万",
"customerContactPerson": "张三",
"jobTitle": "后勤主管",
"customerLevel": "E",
"salesType": "直播",
"intendedproduct": "贴牌",
"status": "不跟进",
"dealMoney": "500万",
"followUpDate": "2023.1.5",
"followUpInformation": [
{
"time": "2023.1.5",
"contents": "客户已付定金"
},
{
"time": "2022.12.5",
"contents": "客户已签合同"
},
{
"time": "2022.11.24",
"contents": "已经和客户确定数量和价格"
},
{
"time": "2023.11.22",
"contents": "初次交流,客户对产品非常满意。待客户回去和公司主管确定购买数量和意向价格"
}
],
"showDetails": false
}
]

2.3 config.json

{
"users": {
"adm": [
{
"name": "aksj666",
"password": "aksj666"
},
{
"name": "aksj888",
"password": "aksj888"
},
{
"name": "15929328625",
"password": "15929328625"
}
],
"common": [
{
"name": "zhangsan",
"password": "zhangsan"
},
{
"name": "zhangsan",
"password": "zhangsan"
},
{
"name": "zhangsan",
"password": "zhangsan"
},
{
"name": "zhangsan",
"password": "zhangsan"
},
{
"name": "zhangsan",
"password": "zhangsan"
},
{
"name": "zhangsan",
"password": "zhangsan"
},
{
"name": "zhangsan",
"password": "zhangsan"
},
{
"name": "zhao12",
"password": "32432"
},
{
"name": "zhao1",
"password": "zhao1"
}
]
},
"regionalManager": [
{
"label": "文淑芳"
},
{
"label": "彭小玉"
},
{
"label": "李姚"
},
{
"label": "贾阳阳"
},
{
"label": "王望"
},
{
"label": "王柳晴"
},
{
"label": "其它人"
}
],
"accessPeoples": [
{
"label": "师宁莉"
},
{
"label": "梅伟"
},
{
"label": "舒瑾"
},
{
"label": "刘晓艺"
},
{
"label": "其它人"
}
]
}

2.4 shangji.js

var shangji = [];

// const shangjiData = require('../data/mock-data/shangji/shangji-data');
const dataUtils = require('../data/data-utils');

var list = {};
list.url = '/sj/shangji/list';
list.type = 'get';
list.handler = function(req, res, next) {
dataUtils.readFile('shangji-lists.json').then((data) => {
res.send(data);
}, (error) => {
res.send(error);
});
};
shangji.push(list);

var create = {};
create.url = '/sj/shangji/list/create';
create.type = 'put';
create.handler = function(req, res, next) {
dataUtils.readFile('shangji-lists.json').then((data) => {
data.unshift(req.body);
dataUtils.writeFile('shangji-lists.json', data).then((data1) => {
res.send(data);
}, (error) => {
res.send(error);
});
}, (error) => {
res.send(error);
});
};
shangji.push(create);

var edit = {};
edit.url = '/sj/shangji/list/edit';
edit.type = 'put';
edit.handler = function(req, res, next) {
dataUtils.readFile('shangji-lists.json').then((data) => {
data.forEach((d, index) => {
if (d.id === req.body.id) {
data.splice(index, 1, req.body);
}
});

dataUtils.writeFile('shangji-lists.json', data).then((data1) => {
res.send(data);
}, (error) => {
res.send(error);
});
}, (error) => {
res.send(error);
});
};
shangji.push(edit);

var del = {};
del.url = '/sj/shangji/list/del';
del.type = 'put';
del.handler = function(req, res, next) {
dataUtils.readFile('shangji-lists.json').then((data) => {
data.forEach((d, index) => {
if (d.id === req.body.id) {
data.splice(index, 1);
}
});

dataUtils.writeFile('shangji-lists.json', data).then((data1) => {
res.send(data);
}, (error) => {
res.send(error);
});
}, (error) => {
res.send(error);
});
};
shangji.push(del);

var me = {};
me.url = '/sj/me';
me.type = 'get';
me.handler = function(req, res, next) {
dataUtils.readFile('config.json').then((data) => {
const params = req.query;
const users = data.users;
const resData = {};
resData.userType = 'guest';
users.common.forEach((c) => {
if (params.name === c.name && params.password === c.password) {
resData.userType = 'common';
}
});
users.adm.forEach((a) => {
if (params.name === a.name && params.password === a.password) {
resData.userType = 'adm';
}
});
res.send(resData);
}, (error) => {
res.send(error);
});
};
shangji.push(me);

var users = {};
users.url = '/sj/users';
users.type = 'get';
users.handler = function(req, res, next) {
dataUtils.readFile('config.json').then((data) => {
const params = req.query;
if (params.userType === 'adm') {
res.send(data);
}
}, (error) => {
res.send(error);
});
};
shangji.push(users);

var regionalManager = {};
regionalManager.url = '/sj/users/regionalManager';
regionalManager.type = 'get';
regionalManager.handler = function(req, res, next) {
dataUtils.readFile('config.json').then((data) => {
res.send(data.regionalManager);
}, (error) => {
res.send(error);
});
};
shangji.push(regionalManager);

var accessPeoples = {};
accessPeoples.url = '/sj/users/accessPeoples';
accessPeoples.type = 'get';
accessPeoples.handler = function(req, res, next) {
dataUtils.readFile('config.json').then((data) => {
res.send(data.accessPeoples);
}, (error) => {
res.send(error);
});
};
shangji.push(accessPeoples);

var usersDel = {};
usersDel.url = '/sj/users/del';
usersDel.type = 'get';
usersDel.handler = function(req, res, next) {
dataUtils.readFile('config.json').then((data) => {
const params = req.query;
if (params.userType !== 'adm') {
return;
}

data.users.common.forEach((d, index) => {
if (d.name === params.name) {
data.users.common.splice(index, 1);
}
});

dataUtils.writeFile('config.json', data).then((data1) => {
res.send(data);
}, (error) => {
res.send(error);
});

}, (error) => {
res.send(error);
});
};
shangji.push(usersDel);

var usersAdd = {};
usersAdd.url = '/sj/users/add';
usersAdd.type = 'get';
usersAdd.handler = function(req, res, next) {
dataUtils.readFile('config.json').then((data) => {
const params = req.query;
if (params.userType !== 'adm') {
return;
}

data.users.common.push({
"name": params.name,
"password": params.password
});

dataUtils.writeFile('config.json', data).then((data1) => {
res.send(data);
}, (error) => {
res.send(error);
});

}, (error) => {
res.send(error);
});
};
shangji.push(usersAdd);

module.exports = shangji;

2.6 inde.html

<script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/xlsx/0.18.2/xlsx.full.min.js"></script>

 

posted on 2019-05-29 17:45  ゛墨メ冰ミ  阅读(476)  评论(0编辑  收藏  举报

导航