forked from koradiyakaushal/Express-CRUD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (68 loc) · 1.89 KB
/
index.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
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
//var expressValidator = require('express-validator');
var mongojs = require('mongojs');
var db = mongojs('customer', ['users']);
var ObjectId = mongojs.ObjectId;
var app = express();
/*
var logger = function(req, res, next){
console.log('logging');
next();
}
app.use(logger);
*/
//view engine
app.set('view engine','ejs');
app.set('views',path.join(__dirname, 'views'));
//body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//set static path
app.use(express.static(path.join(__dirname,'public')));
//global vars
app.use(function(req,res,next){
res.locals.errors = null;
next();
});
app.get('/', function(req,res){
db.users.find(function (err, docs) {
res.render('index',{
title : 'customerApp',
users: docs
});
})
});
app.post('/users/add',function(req,res){
var newUser = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email
}
db.users.insert(newUser, function(err, result){
if(err){
console.log(err);
}
res.redirect('/');
});
});
// app.update('/uses/update/:first_name', function(req, res){
// db.users.update({_id: ObjectId(req.params.)}, function(err, result){
// if(err){
// console.log(err);
// }
// res.redirect('/');
// });
// });
app.delete('/users/delete/:id', function(req, res){
db.users.remove({_id: ObjectId(req.params.id)}, function(err, result){
if(err){
console.log(err);
}
res.redirect('/');
});
})
app.listen(3000, function(){
console.log('connected');
});