forked from OneRedOak/sample-node-couchdb-1
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·43 lines (37 loc) · 1005 Bytes
/
index.js
File metadata and controls
executable file
·43 lines (37 loc) · 1005 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
38
39
40
41
42
var express = require("express"),
nano = require('nano')('http://localhost:5984'),
app = express();
var db = null;
nano.db.destroy('test', function(){
nano.db.create('test', function(err, body){
if(err) {
console.log('Error creating database');
console.log(err);
return;
}
db = nano.db.use('test');
app.listen(3000, function () {
console.log('Express listening on port 3000');
});
});
});
app.get("/", function (req, res) {
res.send("Hey buddy!");
});
app.get("/:name", function (req, res) {
db.get(req.params.name, function (err, body) {
if (body === undefined) {
db.insert({'name': req.params.name}, req.params.name, function(err, b) {
if (err) {
console.log('Error creating a new thing');
console.log(err);
res.send(500);
} else {
res.send({returnObj: b, created:true});
}
});
} else {
res.send({returnObj: body, created:false});
}
});
});