-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (104 loc) · 2.76 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
/*https://github.com/mafintosh/mongojs*/
const mongojs = require('mongojs');
const db = mongojs('expressapp', ['users']);
/*https://github.com/ctavan/express-validate
https://github.com/express-validator/express-validator*/
const expressValidator = require('express-validator');
const app = express();
//Gobal Vars
app.use((req, res, next)=> {
res.locals.errors = null;
next();
});
//View or Template 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
})
);
/*Express Validator Middleware options
This can be copied from express validator middleware*/
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
const namespace = param.split['.'],
root = namespace && namespace.shift(),
formParam = root;
while(namespace && namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param: formParam,
msg: msg,
value: value
}
}
}));
const users = [
{
"first_name": "Omkar",
"last_name": "Atluri",
"id": 1,
"email": "omkar@gmail.com"
},
{
"first_name": "Vamsi",
"last_name": "Atluri",
"id": 2,
"email": "vamsi@gmail.com"
},
{
"first_name": "om",
"last_name": "Atluri",
"id": 3,
"email": "om@gmail.com"
}
];
app.get('/', (req, res)=> {
//res.send(`Home Page path / hit on port 9999`);
// find everything
db.mycollection.find(function (err, docs) {
// docs is an array of all the documents in mycollection
console.log(`users array coming from db collection:` +docs);
//will pass the data to template or view renedering engine
res.render('index', {
'name': 'Omkar',
'users': docs.length > 0 ? docs : users /*Above users array object ref*/
});
});
});
/*Catch the submitted form POST data*/
app.post('/users/add', (req, res) => {
var errors, newUser;
/*Express validator methods or rules before form submission*/
req.checkBody('first_name', 'First Name is Required').notEmpty();
req.checkBody('last_name', 'Last Name is Required').notEmpty();
req.checkBody('email', 'Email is Required').notEmpty();
errors = req.validationErrors();
if (errors) {
console.log(`ERRORS during form submission`);
res.render('index', {
'name': 'Omkar',
'users': users, /*Above users array object ref*/
'errors': errors
});
} else {
newUser = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email
}
console.log(newUser);
}
});
//Set Static Path
app.use(express.static(path.join(__dirname, 'public')));
app.listen(9999, ()=>{
console.log(`Express server listen started on Port 9999`);
});