-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
137 lines (119 loc) · 3.89 KB
/
http.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
const fs = require('fs')
const git = require('simple-git')
const schedule = require('node-schedule');
var express = require('express'),
path = require('path'),
app = express(),
logger = require('morgan'),
multer = require('multer');
var config = {
host: 'localhost',
port: 8008
};
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'www')));
app.set('views', path.join(__dirname, 'www'));
/* GET home page. */
app.get('/', function (req, res, next) {
res.render('index', {
title: '上传页面'
});
});
/* config the multer storage */
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './www/img')
},
filename: function (req, file, cb) {
// cb(null, file.originalname) //用图片原始名字,遇到名字重复会覆盖
// 将保存文件名设置为 时间戳,比如 1478521468943
let suffix = file.mimetype.split('/')[1]; //获取文件格式
cb(null, Date.now() + '.' + suffix);
}
});
var upload = multer({
storage: storage
});
/**
* 初始化git
*/
let gitEntity = git("D:/www/temp/node")
function upDataFile() {
const time = Date()
gitCommit(time)
}
// commit 提交
function gitCommit(time) {
gitEntity.status().then(res => {
const not_added = res.not_added.length === 0;
const created = res.created.length === 0;
const deleted = res.deleted.length === 0;
const modified = res.modified.length === 0;
const renamed = res.renamed.length === 0;
if (not_added && created && deleted && modified && renamed) return false;
gitEntity
.add('./*')
.commit('docs:文档更新')
.push(['-u', 'origin', 'master'], (e) => {
console.log('commit 成功,时间:' + time)
})
})
}
// 取消定时器
function scheduleCancel(){
var counter = 1;
const j = schedule.scheduleJob('* * * * * *', function(){
console.log('定时器触发次数:' + counter);
counter++;
});
setTimeout(function() {
console.log('定时器取消')
// 定时器取消
j.cancel();
}, 5000);
}
// * * * * * *
// │ │ │ │ │ |
// │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
// │ │ │ │ └───── month (1 - 12)
// │ │ │ └────────── day of month (1 - 31)
// │ │ └─────────────── hour (0 - 23)
// │ └──────────────────── minute (0 - 59)
// └───────────────────────── second (0 - 59, OPTIONAL)
function scheduleObjectLiteralSyntax(){
//每分钟的第30秒定时执行一次:
schedule.scheduleJob('10 * * * * *',()=>{
console.log('scheduleCronstyle:' + new Date());
upDataFile()
});
}
//单文件上传,多文件上传参考https://github.com/expressjs/multer
app.post('/upload', upload.single('file'), function (req, res) {
if (req.file) {
res.send('文件上传成功')
console.log(req.file);
/* 输出示例
{
fieldname: 'file', 提交的参数名称,例如需form的input[name=file]
originalname: 'Hydra.jpg',
encoding: '7bit',
mimetype: 'image/png',
destination: './www/img',
filename: 'Hydra.jpg'
path: 'www\\img\\Hydra.jpg',
size: 264794
}*/
console.log(req.body); //其他参数
}
});
app.listen(config.port, config.host, function () {
console.log('Server start on ' + config.host + ':' + config.port);
// // 定时器
// setInterval(function () {
// upDataFile()
// }, 3000000) // 检查一次,本地是否有修改,避免过多commit信息
// // 提交
// upDataFile()
scheduleObjectLiteralSyntax()
});