-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
73 lines (69 loc) · 1.63 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
69
70
71
72
73
var port = process.env.PORT || 5000,
compression = require('compression'),
serveStatic = require('serve-static'),
express = require('express'),
busboy = require('connect-busboy'),
app = express();
app.use(compression());
app.use(serveStatic('docs'));
app.use('/demo', serveStatic('demo'));
app.use('/test', serveStatic('test'));
app.use('/src', serveStatic('src'));
app.use('/dist', serveStatic('dist'));
// for the demo
app.post('/upload-test', busboy({ immediate:true }), function(req, res) {
var fields = {},
i, f;
/*
for (i in req.body) {
if (req.body.hasOwnProperty(i)) {
fields[i] = req.body[i];
}
}
for (i in req.files) {
if (req.files.hasOwnProperty(i)) {
f = req.files[i];
fields[i] = {
name : f.fieldname,
filename : f.originalname,
mimetype : f.mimetype,
size : f.size,
base64 : f.buffer.toString('base64')
};
}
}
*/
if (req.busboy) {
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
var chunks = [],
size = 0;
file.on('data', function(chunk) {
chunks.push(chunk);
size += chunk.length;
});
file.on('end', function() {
fields[fieldname] = {
name : fieldname,
filename : filename,
mimetype : mimetype,
size : size,
base64 : Buffer.concat(chunks, size).toString('base64')
};
});
});
req.busboy.on('field', function(key, value) {
fields[key] = value;
});
req.busboy.on('finish', function() {
res.status(200).json({
fields : fields
});
});
}
else {
res.status(500).text('Not supported');
}
});
app.listen(port, function() {
console.log('Server listening on localhost:'+port);
});