-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
40 lines (33 loc) · 1.01 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
const koa = require('koa');
const mount = require('koa-mount');
const logger = require('koa-logger');
const serve = require('koa-static');
const parse = require('co-busboy');
const fs = require('fs');
const os = require('os');
const path = require('path');
const app = koa();
app.use(logger());
app.use(serve(path.join(__dirname, 'public')));
app.use(mount('/upload', function *(next) {
if ('POST' !== this.method) {
return yield next;
}
var parts = parse(this);
var part;
while (part = yield parts) {
const ext = path.extname(part.filename) || '.png';
const filename = path.join(__dirname, 'public/upload', new Date().getTime() + ext);
const stream = fs.createWriteStream(filename);
part.pipe(stream);
console.log('uploading %s -> %s', part.filename, stream.path);
}
this.set('Access-Control-Allow-Origin', '*');
this.body = {
ret: 0,
msg: 'ok'
};
}));
app.listen(3000, () => {
console.log('listening on port 3000');
});