-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
51 lines (47 loc) · 1.59 KB
/
app.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
var fs = require('fs');
var http = require('http');
var formidable = require('./f/index');
var path = require('path');
var PORT = process.env.VCAP_APP_PORT || 3300;
http.createServer(function(req, res){
if(req.url == '/img') {
var form = new formidable.IncomingForm();
form.uploadDir = path.join(__dirname, "./tmp/");
if(!fs.existsSync(form.uploadDir)) {
fs.mkdir(form.uploadDir);
}
form.parse(req, function(error, fields, files) {
res.writeHead(200, {
"access-control-allow-origin": "*",
"Access-Control-Allow-Methods": "GET, PUT, POST, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With",
"Content-Type": "text/javascript"
});
res.write(JSON.stringify({
code: 200,
path: files.img && files.img.path && files.img.path.split("/").pop()
}));
res.end();
setTimeout(function(){
// 十分钟后删除文件
fs.unlinkSync(files.img.path);
}, 1000 * 60 * 10);
});
} else if(req.url.indexOf('tmp') > -1) {
var filePath = path.join(__dirname, "./" + req.url);
res.writeHead(200, {
"Content-Type": "image/png"
});
if(fs.existsSync(filePath)){
res.write(fs.readFileSync(filePath));
} else {
res.write("<h1><a href='http://barretlee.com/'>小胡子哥</a>告诉你:404了~</h1>");
}
res.end();
} else {
res.write("<h1><a href='http://barretlee.com/'>小胡子哥</a>告诉你:404了~</h1>");
res.end();
}
}).listen(PORT, function(){
console.log('Listen at http://0.0.0.0:' + PORT);
});