-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
116 lines (93 loc) · 3.7 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
var express = require('express');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient; //Retrieve
var ObjectId = require('mongodb').ObjectId;
var app = express();
var db = null;
var bcrypt = require('bcryptjs');
var jwt = require('jwt-simple');
var moment = require('moment');
var JWT_SECRET = 'saurabh';
// Connect to the db
MongoClient.connect("mongodb://saurabh:[email protected]:27017,cluster0-shard-00-01.ksg2h.mongodb.net:27017,cluster0-shard-00-02.ksg2h.mongodb.net:27017/mittens?ssl=true&replicaSet=atlas-3dlq4x-shard-0&authSource=admin&retryWrites=true&w=majority", function(err, dbconn) {
if(!err) {
console.log("We are connected");
db = dbconn.db('meow');
// db = dbconna
}
else {
console.log("not connected!!");
}
});
app.use(bodyParser.json()); // for parsing application/json, you have to include to res.send json
app.use(express.static('public'))
app.get('/meows', function(req, res, next){
db.collection('meows', function(err, meowsCollection) {
meowsCollection.find().toArray(function(err, meows){
return res.json(meows); //can do res.send but see evernote in MEAN notebook for why json was used
});
});
});
app.post('/meows', function(req, res, next){ //next to check for the databse error
console.log(moment().format("dddd, MMMM Do YYYY, h:mm:ss a"));
var token = req.headers['authorization'];
var user = jwt.decode(token, JWT_SECRET); //identifies tweet to a user who was given a token on login
db.collection('meows', function(err, meowsCollection) {
var newMeow = {
text: req.body.newMeow,
user: user._id, // new meows will also have a user ID associated with them, so now we know which user tweeted that tweet
username: user.username,
date: moment().format("h:mma, D MMM YYYY")
};
meowsCollection.insert(newMeow, {w:1}, function(err, meows){
return res.send();
});
});
});
app.put('/meows/remove', function(req, res, next){
var token = req.headers.authorization;
var user = jwt.decode(token, JWT_SECRET); //identifies tweet to a user who was given a token on login
db.collection('meows', function(err, meowsCollection) { //collections are like tables in sql database
var meowId = req.body.meow._id;
meowsCollection.remove({_id: ObjectId(meowId), user: user._id}, {w:1}, function(err, meows){
return res.send();
});
});
});
app.post('/users', function(req, res, next){
db.collection('users', function(err, usersCollection) { //defining a collecction
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(req.body.password, salt, function(err, hash) {
// Store hash in your password DB.
var newUser = {
username : req.body.username,
password : hash
}
usersCollection.insert(newUser, {w:1}, function(err){ //since req.body is an object we can directly add it
return res.send();
});
});
});
});
});
app.put('/users/signin', function(req, res, next){
db.collection('users', function(err, usersCollection) {
usersCollection.findOne({username: req.body.username}, function(err, user){
bcrypt.compare(req.body.password, user.password, function(err, result) {
// result === true
if(result) {
var token = jwt.encode(user, JWT_SECRET); // encode
return res.json({token: token});
}
else {
return res.status(400).send();
}
});
});
});
});
app.listen(process.env.PORT || 3000, function () {
// var d = Date().toString().substring(4,Date().toString().length );
var d= moment().format("h:mma, D MMM YYYY");
console.log(d)
})