-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
107 lines (91 loc) · 2.56 KB
/
Copy pathserver.js
File metadata and controls
107 lines (91 loc) · 2.56 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
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
101
102
103
104
105
106
107
// Generated by CoffeeScript 1.6.3
var BlockModel, app, application_root, blocks, express, mongoose, path, port;
application_root = __dirname;
express = require('express');
path = require('path');
mongoose = require('mongoose');
app = express();
app.configure(function() {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express["static"](path.join(application_root, 'site')));
return app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
mongoose.connect('mongodb://localhost/SteWebSiteDB');
blocks = new mongoose.Schema({
title: String,
description: String,
image: String
});
BlockModel = mongoose.model('Block', blocks);
app.get('/api/blocks', function(request, response) {
return BlockModel.find(function(err, blocks) {
if (!err) {
return response.send(blocks);
} else {
return console.l(err);
}
});
});
app.get('/api/blocks/:id', function(request, response) {
return BlockModel.findById(request.params.id, function(err, block) {
if (!err) {
return response.send(block);
} else {
return console.log(err);
}
});
});
app.put('/api/blocks/:id', function(request, response) {
console.log('Updating block ' + request.body.title);
return BlockModel.findById(request.params.id, function(err, block) {
block.title = request.body.title;
block.description = request.body.description;
block.image = request.body.image;
return block.save(function(err) {
if (!err) {
return console.log('block updated.');
} else {
return console.log(err);
}
return response.send(block);
});
});
});
app["delete"]('/api/blocks/:id', function(request, response) {
console.log('Deleting book with id: ' + request.body.id);
return BlockModel.findById(request.params.id, function(err, block) {
return block.remove(function(err) {
if (!err) {
console.log('Book removed');
return response.send('');
} else {
return console.log(err);
}
});
});
});
app.post('/api/blocks', function(request, response) {
var block;
block = new BlockModel({
title: request.body.title,
description: request.body.description,
image: request.body.image
});
block.save(function(err) {
if (!err) {
return console.log('created');
} else {
return console.log(err);
}
});
return response.send(block);
});
port = 4711;
app.listen(port, function() {
return console.log('Express server listening on port %d in %s mode', port, app.settings.env);
});