-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
150 lines (142 loc) · 5.3 KB
/
Copy pathserver.js
File metadata and controls
150 lines (142 loc) · 5.3 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Require the Express Module
var express = require('express');
// Create an Express App
var app = express();
// Require path
var path = require("path");
// Require body-parser (to receive post data from clients)
var bodyParser = require('body-parser');
// Integrate body-parser with our App
app.use(bodyParser.json());
// Require Mongoose
app.use(express.static(__dirname + '/client/dist'));
var mongoose = require('mongoose');
var uniqueValidator = require('mongoose-unique-validator');
// This is how we connect to the mongodb database using mongoose -- "pet" is the name of
// our db in mongodb -- this should match the name of the db you are going to use for your project.
mongoose.connect('mongodb://localhost/pet');
var PetSchema = new mongoose.Schema({
name: {
type: String,
require: [true, "Name is required!"],
minlength: [3, "Name must be at least 3 characters"],
unique: true
},
type: {
type: String,
require: [true, "Type is required!"],
minlength: [3, "Type must be at least 3 characters"]
},
desc: {
type: String,
require: [true, "Description is required!"],
minlength: [3, "Description must be at least 3 characters"]
},
skill1: {
type: String,
default: ''
},
skill2: {
type: String,
default: ''
},
skill3: {
type: String,
default: ''
},
like: {
type: Number,
default: 0
},
}, { timestamps: true });
PetSchema.plugin(uniqueValidator, { message: 'This name is taken! Please try again.' });
mongoose.model('Pet', PetSchema);
var Pet = mongoose.model('Pet');
// Use native promises
mongoose.Promise = global.Promise;
app.get('/pets', function (req, res) {
Pet.find({}, function (err, pets) {
// console.log(tasks);
if (err) {
console.log("Returned error", err);
// respond with JSON
res.json({ message: "Error", error: err });
} else {
// respond with JSON
res.json({ message: "Success", data: pets });
};
});
});
app.get('/pet/:id', function (req, res) {
Pet.findOne({ _id: req.params.id }, function (err, pet) {
if (err) {
console.log("Returned error", err);
// respond with JSON
res.json({ message: "Error", error: err });
} else {
// respond with JSON
res.json({ message: "Success", data: pet });
};
});
});
app.post('/pet/new', function (req, res) {
var pet = new Pet(req.body);
console.log("new pet", pet);
// Try to save that new eagle to the database (this is the method that actually inserts into the db) and run a callback function with an error (if any) from the operation.
pet.save(function (err) {
// if there is an error console.log that something went wrong!
if (err) {
console.log('something went wrong with new item save');
res.json({ message: "Error", error: err });
} else { // else console.log that we did well and then redirect to the root route
console.log('successfully added to the DB!');
res.json({ message: "Success", data: pet });
};
});
});
app.put('/pet/update', function (req, res) {
var id = req.body._id;
// Try to save that new item to the database (this is the method that actually inserts into the db) and run a callback function with an error (if any) from the operation.
Pet.update({ _id: id }, { name: req.body.name, type: req.body.type, desc: req.body.desc, skill1: req.body.skill1, skill2: req.body.skill2, skill3: req.body.skill3 }, function (err) {
// if there is an error console.log that something went wrong!
if (err) {
console.log('something went wrong with new item save');
res.json({ message: "Error", error: err });
} else { // else console.log that we did well and then redirect to the root route
console.log('successfully updated the DB!');
res.json({ message: "Success" });
};
});
});
app.put('/like/update', function (req, res) {
var like = req.body;
Pet.update({ _id: like[0] }, { $set: { "like": like[1] } }, function (err) {
// if there is an error console.log that something went wrong!
if (err) {
console.log('something went wrong with new item save');
res.json({ message: "Error", error: err });
} else { // else console.log that we did well and then redirect to the root route
console.log('successfully updated the DB!');
res.json({ message: "Success" });
};
});
});
app.delete('/pet/remove/:id', function (req, res) {
Pet.remove({ _id: req.params.id }, function (err) {
if (err) {
console.log('something went wrong with save');
res.json({ message: "Error", error: err });
} else { // else console.log that we did well and then redirect to the root route
console.log('successfully removed record!');
res.json({ message: "Success" });
};
});
});
//Catch all routing
app.all("*", (req, res, next) => {
res.sendFile(path.resolve("./client/dist/index.html"))
});
// Setting our Server to Listen on Port: 8022
app.listen(8022, function () {
console.log("listening on port 8022");
});