-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (79 loc) · 2.35 KB
/
Copy pathapp.js
File metadata and controls
86 lines (79 loc) · 2.35 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
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
/*include the nano module*/
var nano = require('nano')('http://localhost:5984');
/*Specify the database to use, so that we don't have to specify everytime*/
var db = nano.use('address')
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.post('/createdb', function(req, res){
nano.db.create(req.body.dbname,function(err) {
// create a new database
if (err) {
res.send("Error creating Database "+req.body.dbname);
return;
}
res.send("Database "+req.body.dbname+" was created sucessfully");
});
});
app.post('/new_contact', function(req, res){
var name=req.body.name;
var phone=req.body.phone;
/*The second parameter phone is the id we are explicitly specifying*/
db.insert({name:name,phone:phone, crazy: true },phone,function(err, body, header) {
if (err) {
res.send("Error creating contacts or contacts already exists");
return;
}
res.send("Contacts was created sucessfully");
});
});
app.post('/view_contact', function(req, res){
var alldoc="Following are the Docment <br/><br/>";
db.get(req.body.phone, { revs_info: true }, function(err, body) {
if (!err)
console.log(body);
if(body){
alldoc+="Name:"+body.name +"<br/> Phone :"+body.phone;
}else{
alldoc="No Record exist with that key";
}
res.send(alldoc);
});
});
app.post('/delete_contact', function(req, res){
db.get(req.body.phone, { revs_info: true }, function(err, body) {
if (!err){
db.destroy(req.body.phone,body._rev , function(err, body) {
if (!err){
res.send("Error in deleting");
}else{
}
});
res.send("Document deleted sucessfully");
}
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});