-
Notifications
You must be signed in to change notification settings - Fork 0
/
express_server.js
136 lines (114 loc) · 3.84 KB
/
express_server.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
var express = require('express');
var app = express();
var formidable = require('formidable');
var fs = require('fs');
var path = require("path");
var baseImagePath = __dirname + "\\image\\";
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:8081');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
var urlMap = {
"http://127.0.0.1:8081/file/test1" : "pic1",
"http://127.0.0.1:8081/file/test2" : "pic2"
};
function getBaseDir() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
return baseImagePath + year + "\\" + month;
}
//递归创建目录 异步方法
function mkdirs(dirname, callback) {
fs.exists(dirname, function (exists) {
if (exists) {
callback();
} else {
//console.log(path.dirname(dirname));
mkdirs(path.dirname(dirname), function () {
fs.mkdir(dirname, callback);
});
}
});
}
//递归创建目录 同步方法
function mkdirsSync(dirname) {
//console.log(dirname);
if (fs.existsSync(dirname)) {
return true;
} else {
if (mkdirsSync(path.dirname(dirname))) {
fs.mkdirSync(dirname);
return true;
}
}
}
function checkDirAndFile(dirPath, success) {
if (fs.existsSync(dirPath)) {
console.log('已经创建过此更新目录了');
success();
} else {
console.log("check" + dirPath);
if(mkdirsSync(dirPath)) {
success();
console.log('更新目录已创建成功\n');
}
}
}
app.get('/index.html', function(req, res) {
console.log("get index");
res.sendFile( __dirname + "/" + "index.html" );
});
// /[image]+\/\d+\/\d+\/\d+@\w+.\w+/
app.get(/[image]+\/\d\d\d\d\/\d+\/\d+@\w+.\w+/, function(req, res) {
console.log("get jpg " + req.path);
//上传的图片不要有中文
res.sendFile(__dirname + "/" + req.path);
});
app.get('list.txt', function(req, res) {
console.log("get list request");
res.send(JSON.stringify(urlMap));
});
app.post('/file_upload', function (req, res) {
debugger;
var form = new formidable.IncomingForm();
form.uploadDir = __dirname + "\\defualt";
form.keepExtensions = true;
form.on('fileBegin', function(name, file) {
//在这个时间触发后,文件已经被存储到文件系统中了。
var now = new Date();
file.name = now.getTime() + "@" + file.name;
file.path = baseImagePath + file.name;
var tPath = getBaseDir().toString();
// var tPath = "test\\test1";
//console.log(tPath);
checkDirAndFile(tPath, function() {
file.path = tPath + "\\" + file.name;
});
//console.log("I'm on fileBegin " + JSON.stringify(file));
});
form.on('file', function(field, file) {
//rename the incoming file to the file's name
//fs.rename(file.path, __dirname + "\\image\\" + file.name);
//console.log("I'm on file " + JSON.stringify(file));
});
form.parse(req, function(err, fields, files) {
//console.log("I'm parse " + JSON.stringify(files));
}
);
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})