-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebserver.js
More file actions
62 lines (56 loc) · 1.77 KB
/
webserver.js
File metadata and controls
62 lines (56 loc) · 1.77 KB
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
var express = require('express');
var util = require("util");
var WebServer = {
init: function () {
this.app = express();
this.app.use(express.bodyParser());
this.app.get('/packages', function(req, res){
this.pkg.findAll({order: 'name DESC'}).success(function(packages) {
res.send(packages);
});
}.bind(this));
this.app.post('/packages', function (req, res) {
var name, url, pkg;
name = req.param('name');
url = req.param('url');
pkg = this.pkg.build({name: name, url: url});
var errors = pkg.validate();
if(!errors){
pkg.save().success(function () {
res.send(200);
}).error(function (e) {
res.send(406);
});
}
else{
console.log(errors);
res.send(400);
}
}.bind(this));
this.app.get('/packages/:name', function (req, res) {
var name = req.params.name;
this.pkg.find({where: ["name = ?", name]}).success(function(pkg) {
if(pkg){
pkg.hit();
res.send(pkg.toJSON());
}
else{
res.send(404);
}
});
}.bind(this));
this.app.get('/packages/search/:name', function (req, res) {
var name = req.params.name;
this.pkg.findAll({where: ["name ilike ?", '%'+name+'%'], order: 'name DESC'}).success(function(packages) {
res.send(packages);
});
}.bind(this));
return this;
},
listen: function (port) {
this.pkg = this.app.get('pkg');
this.app.listen(port);
return this;
}
};
module.exports = Object.create(WebServer);