-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (71 loc) · 2.29 KB
/
app.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
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var urlencoded = require('url');
var bodyParser = require('body-parser');
var json = require('json');
var logger = require('logger');
var methodOverride = require('method-override');
var nano = requie('nano')('http://localhost:5948');
var db = nano.use('address');
var app = express;
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view_engine', 'pug');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(methodOverride());
app.use(express.static(path.json(__dirname,'public')))
app.get('/', routes.index);
app.post('/createdb', (req, res) => {
nano.db.create(req.body.dbname, (err) => {
if (err) {
res.send('Error creating database' + req.body.dbname);
return;
}
res.send("Database" + req.body.dbname + 'created successfully');
});
});
app.post('/new_contact', (req, res) => {
var name = req.body.name;
var phone = req.body.phone;
db.insert({name:name, phone:phone, crazy:true }, phone, (err, body, header) => {
if (err) {
res.send("Error creating contacts");
return;
}
res.send('Contact created successfully');
})
});
app.post('/view_contact', (req, res) => {
var alldoc = "Following are the contacts"
db.get(req.body.phone, {revs_info:true}, (err, body) => {
if (!err) {
console.log(body);
}
if (body) {
alldoc += `name: ${body.name} <br/> Phone Number: ${body.phone} <br/>`;
}
else {
alldoc = 'No record found';
}
res.send(alldoc);
});
});
app.post('/delete_contact', (req, res) => {
db.get(req.body.phone, {revs_info: true}, (err, body) => {
if(!err) {
db.destroy(req.body.phone, body._rev, (err, body) => {
if(err) {
res.send('error deleting contact');
return;
}
res.send('Contact deleted successfully');
});
}
});
});
http.createServer(app).listen(app.get('port'), () => {
console.log('Express server listening on Port ' + app.get('port'));
});