-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (64 loc) · 2.54 KB
/
index.js
File metadata and controls
77 lines (64 loc) · 2.54 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
// require express and body parser modules//
const express = require('express');
const bodyParser = require('body-parser');
const path=require('path');
// creating mongo instance//
const MongoClient = require('mongodb').MongoClient;
// c9 local mongodb url
//const url = 'mongodb://chamsudhir-wallapp-4894806:27017/newdb';
// heroku mongodb url
const url = 'mongodb://chamsudhir-wallapp-4894806:27017/newdb';
// creating the app server using function express //
const app = express();
app.use(express.static(path.join(__dirname,'public')));
app.use(bodyParser.urlencoded({extended : true}));
app.set('view engine', 'ejs');
//connect to mongodb//
MongoClient.connect(url, function(err,db){
if(err) return console.log(err);
// server listening on port//
app.listen(process.env.PORT, function(){
console.log("listening to c9 port");
});
// serve the index file to the server//
app.get('/',function(req,res){
db.collection('quotes').find().toArray(function(err,result){
if(err) return console.log(err);
res.render('quotes.ejs', {quotes: result});
});
});
app.post('/addQ', function(req,res){
db.collection('quotes').save(req.body, function(err,result){
if(err) return console.log(err);
console.log('quote saved to database');
res.redirect('/');
});
});
app.post('/home', function(req,res){
res.redirect('/');
});
var fnameO;
app.post('/query', function(req, res){
fnameO = req.body.fname;
console.log('fname in post-query: ' + fnameO );
res.redirect('/update');
});
app.get('/update', function(req, res){
console.log('fname in get-query: ' + fnameO );
db.collection('quotes').find({name: fnameO}).toArray(function(err,result){
if(err) return console.log(err);
console.log(result);
res.render('update.ejs', {quotes: result});
});
});
app.post('/deleteQ', function(req, res) {
console.log("in deleteQ");
console.log(fnameO +' ' + req.body.deleteB);
db.collection('quotes').remove({name: fnameO,message: req.body.deleteB});
res.redirect('/update');
});
app.post('/updateQ', function(req, res) {
db.collection('quotes').update({name: fnameO,message: req.body.updateB}, {name: fnameO,message: req.body.updatedQ});
res.redirect('/update');
});
});