forked from ryan-roemer/backbone-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
149 lines (128 loc) · 3.36 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Simple MongoDB-based backend notes server.
*/
var
// Mongoose
mongoose = require("mongoose"),
MONGO_ADDR = process.env.MONGO_ADDR || "127.0.0.1",
MONGO_PORT = parseInt(process.env.MONGO_PORT || 27027, 10),
Note = {},
// Express.
express = require("express"),
app = express(),
ADDR = process.env.ADDR || "127.0.0.1",
PORT = parseInt(process.env.PORT || 4321, 10);
// -----------------------
// Notes Model
// -----------------------
Note.Schema = mongoose.Schema({
title: {
type: String,
trim: true,
"default": ""
},
text: {
type: String,
trim: true,
"default": "*Edit your note!*"
},
createdAt: {
type: Date,
"default": new Date()
}
}, {
// Remove extra `id` attribute so we can make virtual.
// See: https://github.com/LearnBoost/mongoose/issues/1137
id: false,
// Add virtual fields to data here.
toJSON: { virtuals: true },
toObject: { virtuals: true }
});
Note.Schema.virtual("id").get(function () {
return this.get("_id");
});
Note.Model = mongoose.model("Note", Note.Schema);
// -----------------------
// Application helpers
// -----------------------
// Generic results handler.
function _handler(res, status) {
return function (err, results) {
if (err) {
return res.json({ err: err.toString() }, 500);
}
// Translate to JSON.
if (Array.isArray(results)) {
results = results.map(function (m) {
return m.toJSON();
});
} else {
results = results.toJSON();
}
res.set("cache-control", "no-cache");
res.json(results, status || 200);
};
}
// Log requests.
function _logRequest(req, res, next) {
console.log([
req.method,
req.url,
JSON.stringify(req.body)
].join(" "));
return next();
}
// Redirects.
function _redirect(dest) {
return function (req, res) {
res.redirect(dest);
};
}
// -----------------------
// Application server
// -----------------------
// Connect to MongoDB.
mongoose.connect("mongodb:https://" + MONGO_ADDR + ":" +
MONGO_PORT + "/test");
// Ignore favicon.
app.use(function (req, res, next) {
if (req.url !== "/favicon.ico") { return next(); }
res.set({"Content-Type": "image/x-icon"});
res.send(200);
});
// Configurations and static server.
app.use(express.bodyParser());
app.enable("strict routing");
// Application.
app.all(/^(|\/|\/app)$/, _redirect("/app/"));
app.use("/app/", express.static(__dirname + "/app"));
// Tests.
app.all(/^\/test(|\/)$/, _redirect("/test/test.html"));
app.use("/test/", express.static(__dirname + "/test", {
redirect : false
}));
app.use(_logRequest);
// REST API
// Notes Collection
app.get("/api/notes", function (req, res) { // (R)ead
Note.Model.find({}, _handler(res));
});
// Note Model
app.post("/api/notes", function (req, res) { // (C)reate
Note.Model.create(req.body, _handler(res, 201));
});
app.put("/api/notes/:id", function (req, res) { // (U)pdate
Note.Model.findByIdAndUpdate(req.param("id"), { "$set": {
title: req.body.title,
text: req.body.text
}}, _handler(res));
});
app.get("/api/notes/:id", function (req, res) { // (R)ead
Note.Model.findById(req.param("id"), _handler(res));
});
app.del("/api/notes/:id", function (req, res) { // (D)elete
Note.Model.findByIdAndRemove(req.param("id"), _handler(res));
});
// Run server.
app.listen(PORT, ADDR);
console.log("Server started up at http:https://" + ADDR + ":" + PORT);