主题
今天的主题是个人项目
主要内容
还是以昨天那个为主吧,说一下设计思路.
首先我们来看一下文件结构
D:.
├─.vscode // vscode配置文件
├─bin // 执行脚本
├─config // 配置文件
├─public // 页面文件
├─routes // 路由文件
├─services // 业务逻辑目录
├─test // 测试目录
├─upload // 上传文件的零时文件夹
│ └─images
└─utils // 工具函数目录
思路
约束导入导出
所有的js
对象都通过一个index.js
文件导出,引入时都使用按需引入.例如utils/index.js
文件如下:
const helper = require("./helper");
const jsonRes = require("./jsonRes");
const mailer = require("./mailer");
const uploader = require("./uploader");
module.exports = {
Helper: helper,
JsonResponse: jsonRes,
Mailer: mailer,
Uploader: uploader,
};
尽最大可能不影响引用工具函数的文件,让变化局限在index.js
和变更的文件中!
配置隔离
类似于后端服务的配置管理都尽量单独放在文件中,而不是零散的放在各个文件.应该使用专门的配置文件来管理.例如这里的config/index.js
,内容如下:
module.exports = {
mailer: {
account: "",
password: "",
default: {
receiver: "",
subject: "",
},
},
aliyun: {
account: {
imageRecognize: {
accessKeyId: "put your access key Id",
accessKeySecret: "put you access key secret",
},
oss: {
accessKeyId: "put your access key Id",
accessKeySecret: "put you access key secret",
buckets: {
shanghai: {
region: "oss-cn-shanghai",
bucketName: "put your shanghai oss bucket name",
},
},
},
},
recognizeInterface: {
recognizeVehicle: {
endpoint: "https://imagerecog.cn-shanghai.aliyuncs.com",
type: "RecognizeVehicleType",
apiVersion: "2019-09-30",
},
recognizeVehiclePlate: {
endpoint: "https://ocr.cn-shanghai.aliyuncs.com",
type: "RecognizeLicensePlate",
apiVersion: "2019-12-30",
},
recognizeDriverLicense: {
endpoint: "https://ocr.cn-shanghai.aliyuncs.com",
type: "RecognizeDriverLicense",
apiVersion: "2019-12-30",
sideRequired: true,
},
recognizeIdentityCard: {
endpoint: "https://ocr.cn-shanghai.aliyuncs.com",
type: "RecognizeIdentityCard",
apiVersion: "2019-12-30",
sideRequired: true,
},
recognizeDrivingLicense: {
endpoint: "https://ocr.cn-shanghai.aliyuncs.com",
type: "RecognizeDrivingLicense",
apiVersion: "2019-12-30",
sideRequired: true,
},
},
},
};
像邮箱,阿里云的配置appid,appsecret
都放在统一的文件下,便于管理和变更.
还有其他的例如使用prettier
来规范代码样式,使用eslint
来规范代码习惯,使用mocha
来进行自动化单元测试.