-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
150 lines (115 loc) · 2.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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('workOrderApp', ['Jobs']);
var app = express();
//Middleware basic example
/*
var logger = function(req, res, next){
console.log('Logging...');
next();
}
app.use(logger);
*/
//Veiw Engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
//Body Paser 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();
});
// Express Validator
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
app.get('/', function(req, res){
db.Jobs.find(function (err, docs) {
// docs is an array of all the documents in mycollection
console.log(docs);
res.render('index', {
title: 'Jobs',
jobs: docs
});
});
});
// Table of jobs in workOrderApp collection
app.get('/tables', function(req, res){
db.Jobs.find(function(err, docs){
res.render('tables', {
title: 'Tables',
jobs: docs
});
});
});
// Filter based on location
app.post('/jobs/filter', function(req, res){
var filter_id = {
job_location: req.body.sort_selection
};
console.log(filter_id);
db.Jobs.find(filter_id).toArray(function(err, docs){
res.render('tables', {
title: 'Tables',
jobs: docs
});
});
});
// Add new users
app.post('/jobs/add', function(req, res){
//console.log(req.body.first_name);
req.checkBody('job_id', 'Job Id is required ').notEmpty();
req.checkBody('job_location', 'Job Location is required ').notEmpty();
req.checkBody('job_description', 'Job Description is required ').notEmpty();
req.checkBody('job_notes', 'Job Notes is required ').notEmpty();
var errors = req.validationErrors();
db.Jobs.find(function (err, docs) {
if(errors){
console.log('ERRORS');
res.render('index', {
title: 'Jobs',
jobs: docs,
errors: errors
});
} else {
console.log('newjob');
var newJob = {
job_id: req.body.job_id,
job_location: req.body.job_location,
job_description: req.body.job_description,
job_notes: req.body.job_notes,
job_date: req.body.job_date
};
//Add user to MongoDB
db.Jobs.insert(newJob, function(err, result){
if(err){
console.log(err);
}
res.redirect('/');
});
}
console.log(newJob);
});
});
app.listen(3000, function(){
console.log('Server Started on port 3000...');
});