forked from shippableSamples/sample_node_mongo
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (32 loc) · 828 Bytes
/
index.js
File metadata and controls
37 lines (32 loc) · 828 Bytes
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
var express = require("express"),
mongoose = require("mongoose"),
app = express();
mongoose.connect("mongodb://localhost/test", function (err) {
if (!err) {
console.log("Connected to MongoDB");
} else {
console.error(err);
}
});
app.get("/", function (req, res) {
res.send("Hey buddy!");
});
var Thing = require("./model");
app.get("/:name", function (req, res) {
Thing.find({ name: req.params.name }, function (err, t) {
if (t.length < 1) {
var thing = new Thing();
thing.name = req.params.name;
thing.save(function(err, savedThing) {
if (err) {
res.send(500);
} else {
res.send({returnObj: savedThing, created: true});
}
});
} else {
res.send({returnObj: t[0], created: false});
}
});
});
app.listen(3000);