-
Notifications
You must be signed in to change notification settings - Fork 59
/
app.js
90 lines (75 loc) · 1.87 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
90
const express = require('express');
const exphbs = require('express-handlebars');
const path = require('path');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const redis = require('redis');
// Create Redis Client
let client = redis.createClient();
client.on('connect', function(){
console.log('Connected to Redis...');
});
// Set Port
const port = 3000;
// Init app
const app = express();
// View Engine\
app.engine('handlebars', exphbs({defaultLayout:'main'}));
app.set('view engine', 'handlebars');
// body-parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
// methodOverride
app.use(methodOverride('_method'));
// Search Page
app.get('/', function(req, res, next){
res.render('searchusers');
});
// Search processing
app.post('/user/search', function(req, res, next){
let id = req.body.id;
client.hgetall(id, function(err, obj){
if(!obj){
res.render('searchusers', {
error: 'User does not exist'
});
} else {
obj.id = id;
res.render('details', {
user: obj
});
}
});
});
// Add User Page
app.get('/user/add', function(req, res, next){
res.render('adduser');
});
// Process Add User Page
app.post('/user/add', function(req, res, next){
let id = req.body.id;
let first_name = req.body.first_name;
let last_name = req.body.last_name;
let email = req.body.email;
let phone = req.body.phone;
client.hmset(id, [
'first_name', first_name,
'last_name', last_name,
'email', email,
'phone', phone
], function(err, reply){
if(err){
console.log(err);
}
console.log(reply);
res.redirect('/');
});
});
// Delete User
app.delete('/user/delete/:id', function(req, res, next){
client.del(req.params.id);
res.redirect('/');
});
app.listen(port, function(){
console.log('Server started on port '+port);
});