forked from nagypeter/nodejs-mongodb-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
92 lines (79 loc) · 2.1 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
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, EmployeeProvider = require('./employeeprovider').EmployeeProvider;
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {layout: false});
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.enable('trust proxy');
});
app.configure('development', function(){
app.use(express.errorHandler());
});
var employeeProvider= new EmployeeProvider();
//Routes
//index
app.get('/', function(req, res){
employeeProvider.findAll(function(error, emps){
res.render('index', {
title: 'Employees',
employees:emps
});
});
});
//new employee
app.get('/employee/new', function(req, res) {
res.render('employee_new', {
title: 'New Employee'
});
});
//save new employee
app.post('/employee/new', function(req, res){
employeeProvider.save({
name: req.param('name'),
title: req.param('title')
}, function( error, docs) {
res.redirect('/')
});
});
//update an employee
app.get('/employee/:id/edit', function(req, res) {
employeeProvider.findById(req.params.id, function(error, employee) {
res.render('employee_edit',
{
_id: employee._id.toHexString(),
name: employee.name,
title: employee.title,
});
});
});
//save updated employee
app.post('/employee/:id/edit', function(req, res) {
employeeProvider.update(req.params.id,{
name: req.param('name'),
title: req.param('title')
}, function(error, docs) {
res.redirect('/')
});
});
//delete an employee
app.get('/employee/:id/delete', function(req, res) {
employeeProvider.delete(req.params.id, function(error, docs) {
res.redirect('/')
});
});
app.listen(process.env.PORT || 3000);