-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
117 lines (107 loc) · 3.58 KB
/
server.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
// https://codequs.com/p/B108Pqbt/build-a-restful-api-with-node-js-and-mongodb/
// coverage test: "istanbul cover node_modules\mocha\bin\_mocha -- -R spec"
//hot deploy: nodemon ./server.js
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoOp = require("./models/mongo");
var router = express.Router();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ "extended": false }));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use("/scripts", express.static("./client/js"));
app.use("/css", express.static("./client/css"));
app.use("/images", express.static("./client/images"));
app.use("/public", express.static("./client/public"));
router.get("/", function (req, res) {
//res.json({ "status": 200, "message": "Hello World" });
res.render('index', { title: 'Express + JADE', content: 'Hello JADE, My name is tommyspot' });
});
router.route("/users")
.get(function (req, res) {
var response = {};
mongoOp.find({}, function (err, data) {
if (err) {
response = { status: 404, message: "Error fetching data" };
} else {
response = { status: 200, data: data };
}
res.json(response);
//res.render('users', response);
});
})
.post(function (req, res) {
var db = new mongoOp();
var response = {};
if(req.body.email === '' || req.body.password === ''){
response = { status: 501, message: "Input fields must not empty" };
res.json(response);
} else {
db.userEmail = req.body.email;
db.userPassword = require('crypto').createHash('sha1').update(req.body.password).digest('base64');
db.save(function (err, data) {
if (err) {
response = { status: 501, message: "Error adding data" };
} else {
response = { status: 200, data: data };
}
res.json(response);
});
}
});
router.route("/users/:id")
.get(function (req, res) {
var response = {};
mongoOp.findById(req.params.id, function (err, data) {
if (err) {
response = { status: 501, message: "Error fetching data" };
} else {
response = { status: 200, data: data };
}
res.json(response);
});
})
.put(function (req, res) {
var response = {};
mongoOp.findById(req.params.id, function (err, data) {
if (err) {
response = { status: 501, message: "Error fetching data" };
} else {
if (req.body.email !== undefined) {
data.userEmail = req.body.email;
}
if (req.body.password !== undefined) {
data.userPassword = require('crypto').createHash('sha1').update(req.body.password).digest('base64');
}
data.save(function (err, data) {
if (err) {
response = { status: 501, message: "Error updating data" };
} else {
response = { status: 200, data: data };
}
res.json(response);
})
}
});
})
.delete(function (req, res) {
var response = {};
mongoOp.findById(req.params.id, function (err, data) {
if (err) {
response = { status: 501, message: "Error fetching data" };
} else {
mongoOp.remove({ _id: req.params.id }, function (err) {
if (err) {
response = { status: 501, message: "Error deleting data" };
} else {
response = { status: 200, message: "Data associated with " + req.params.id + "is deleted" };
}
res.json(response);
});
}
});
})
app.use('/', router);
app.listen(process.env.PORT || 3000);
console.log("Listening to PORT 3000");