-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
100 lines (87 loc) · 2.28 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var config = require('./config');
var express = require('express');
var Memowizer = require('./lib/memowizer');
var Validator = require('./lib/validator');
var Wemember = require('./lib/wemember');
var app = express();
app.configure(function(){
app.use(express.bodyParser());
app.use(express.static(__dirname + '/' + config.dir));
app.use(app.router);
});
app.get('/', function (req, res) {
res.send('Welcome to the ' + config.name);
});
app.del('/rm', function (req, res) {
var path = req.param('path').split('/');
Wemember.create(path.slice(0, -1).join('/'), config.dir)
.rm(path.slice(-1)).then(function () {
res.send(410);
}, function (err) {
console.log(
'error: {path: "' + path.join('/') + '"} - ' + err.toString()
);
res.send(404);
});
});
app.del('/rmdir', function (req, res) {
var path = req.param('path');
Wemember.create(path, config.dir)
.rmdir().then(function () {
res.send(410);
}, function (err) {
console.log(
'error: {path: "' + path + '"} - ' + err.toString()
);
res.send(409);
});
});
app.del('/rmdir', function (req, res) {
});
app.get('/ls', function (req, res) {
var path = req.param('path');
Wemember.create(path, config.dir).getContents().then(function (files) {
res.send({
contents: files,
path: path
});
}, function (err) {
console.log(
'error: {path: "' + path + '"} - ' + err.toString()
);
res.send(404);
});
});
app.post('/webowize', function (req, res) {
var props = {
name: req.param('name'),
url: req.param('url')
};
if (!Validator.create(props).isValid()) {
console.log(
'error: {name: "' + props.name + '", url: "' +props.url +
'"} - invalid'
);
res.send({
err: 'invalid parameters'
}, 400);
} else {
Memowizer.create(props.name, props.url, config.dir)
.save()
.then(function () {
res.send({
path: props.name
}, 201);
}, function (err) {
console.log(
'error: {name: "' + props.name + '", url: "' + props.url +
'"} - ' + err
);
res.send({
err: err
}, 500);
});
}
});
app.listen(config.server.port);
console.log(config.name + ' is listening on port ' + config.server.port);