forked from react-component/upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (59 loc) · 1.41 KB
/
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
'use strict';
const app = require('rc-tools/lib/server/')();
const parse = require('co-busboy');
const fs = require('fs');
function wait(time) {
return function (callback) {
setTimeout(callback, time);
}
}
app.post('/upload.do', function*() {
try {
const parts = parse(this, {
autoFields: true
});
let part, files = [];
while (part = yield parts) {
files.push(part.filename);
part.resume();
}
let ret = '';
this.status = 200;
this.set('Content-Type', 'text/html');
yield wait(2000);
if (parts.fields[0] && parts.fields[0][0] === '_documentDomain') {
ret += '<script>document.domain="' + parts.fields[0][1] + '";</script>';
}
ret += JSON.stringify(files);
console.log(ret);
this.body = ret;
} catch (e) {
this.body = e.stack;
}
});
app.post('/test', function*() {
this.set('Content-Type', 'text/html');
const parts = parse(this, {
autoFields: true
});
let part;
const files = [];
while (part = yield parts) {
files.push(part.filename);
part.resume();
}
const ret = parts.fields[2];
if (ret[1].indexOf('success') > -1) {
this.status = 200;
this.body = ret;
} else {
this.status = 400;
this.body = 'error 400';
}
});
const port = process.env.npm_package_config_port;
app.listen(port);
console.log('listen at ' + port);
process.on('uncaughtException', err => {
console.log(err.stack);
})