-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (47 loc) · 1.27 KB
/
index.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
var express = require('express');
var path = require('path');
var mkdirp = require('mkdirp');
var fs = require('fs');
var Servion = function(opts){
if(!(this instanceof Servion)){
return new Servion(opts);
}
opts.backend.listen(opts.port);
}
Servion.disk = function(opts){
var app = express();
if(opts.cors){
console.log("CORS!")
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
}
app.use('/', express.static(opts.path));
app.post('*', function(req, res, next){
console.log(req.path);
var dst = path.join(opts.path, req.path);
mkdirp(path.dirname(dst), function(err){
if(err){
res.status(500).send(err.toString());
}
var file = fs.createWriteStream(dst);
req.pipe(file);
req.once('error', function(err){
res.status(500).send(err.toString());
});
file.once('error', function(err){
res.status(500).send(err.toString());
});
file.once('finish', function(){
res.status(201).end();
});
});
});
this.listen = function(port){
app.listen(port);
}
return this;
}
module.exports = Servion;