-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
55 lines (46 loc) · 1.41 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
/*eslint no-console: ["error", { allow: ["warn", "error", "log"] }] */
let fs = require('fs');
let config = require('./config');
/*let randomString = (length) => {
let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let res = '';
for(let i = 0; i < length; i++)
res += possible.charAt(Math.floor(Math.random() * possible.length));
return res;
};*/
let log = (content) => {
console.log(content);
content += '\n';
config.log && fs.appendFile(config.log, content, (err) => {
if (err) {
fs.writeFile(config.log, content, () => {});
}
});
};
let logTool = (req, res, next) => {
let content = `[${new Date().toLocaleString()}] ${req.headers['x-real-ip'] || req.ip}: ${req.method} ${req.url}`;
log(content);
next();
};
let md5 = (content) => {
return require('crypto').createHash('md5').update(content, 'utf8').digest('hex');
};
let dateFormatter = new Intl.DateTimeFormat('zh-CN', {
timeZone: 'Asia/Shanghai',
hour12: false
});
let timeFormatter = new Intl.DateTimeFormat('zh-CN', {
timeZone: 'Asia/Shanghai',
hour12: false,
hour: 'numeric', minute: 'numeric', second: 'numeric'
});
let formatDate = (date) => {
return dateFormatter.format(date);
};
let formatTime = (time) => {
return timeFormatter.format(time);
};
module.exports = {
md5, logTool, log, formatDate, formatTime
};