-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (35 loc) · 929 Bytes
/
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
var nconf = require('nconf');
var express = require('express');
var cors = require('cors');
var fs = require('fs');
/*
* get config from file
*/
nconf.file({ file: 'config.json' });
/*
* static media files
*/
var app = express();
// enable CORS
app.use(cors());
// serve static files
app.use(express.static(nconf.get('uploads').path));
app.use(express.multipart());
function saveAttachement(fields, files) {
var uuid = JSON.parse(fields.meta).uuid;
var destination = nconf.get('uploads').path + '/' + uuid;
var is = fs.createReadStream(files.file.path);
var os = fs.createWriteStream(destination);
is.pipe(os);
is.on('end',function() {
fs.unlinkSync(files.file.path);
});
}
app.post('/', function(req, res) {
res.writeHead(200);
res.end(saveAttachement(req.body, req.files));
});
var port = nconf.get('server').port;
app.listen(port, function() {
console.log('listening on ' + port);
});