This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
131 lines (94 loc) · 2.87 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Load required packages
var express = require("express");
var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var Player = require("./models/player");
// Connect to the database
mongoose.connect("mongodb://localhost/soccr"); // name can be whatever you need it to be
// Create the Express application
var app = express();
// Use environment defined port or 3000
var port = process.env.PORT || 3000;
// Use the body-parser package in our application
app.use(bodyParser.json());
app.use(express.static(__dirname + "/public"));
// READ
// Create endpoint /api/players for POST
app.post("/api/players", function (req, res) {
// Create a new instance of the Player model
var player = new Player();
// Set the player properties that came from the POST data
player.name = req.body.name;
player.team = req.body.team;
player.number = req.body.number;
player.position = req.body.position;
// Save the player and check for errors
player.save(function (err) {
if (err) {
res.send(err);
}
res.json(player);
});
});
// Create endpoint /api/players for GET
app.get("/api/players", function(req, res) {
// Use the Player model to find all players
Player.find(function (err, players) {
if (err) {
res.send(err);
}
res.json(players);
});
});
// CREATE
// Create endpoint for /api/players/:playerID
app.get("/api/players/:player_id", function(req, res) {
// Use the player model to find a specific player
Player.findById(req.params.player_id, function (err, player) {
if (err) {
res.send(err);
}
res.json(player);
});
});
// UPDATE
// Change the player's number
app.put("/api/players/:player_id", function(req, res) {
// Use the Player model to find a specific player
Player.findById(req.params.player_id, function (err, player) {
if (err) {
res.send(err);
}
// Update the player's number
player.name = req.body.name;
player.team = req.body.team;
player.number = req.body.number;
player.position = req.body.position;
// Save the player and check for errors
player.save(function (err) {
if (err) {
res.send(err);
}
res.json(player);
});
});
});
// DELETE
// Create endpoint /api/players/:player_id for DELETE
app.delete("/api/players/:player_id", function (req, res) {
// Use the player model to find a specific player and remove it
Player.findByIdAndRemove(req.params.player_id, function(err) {
if (err) {
res.send(err);
}
res.json({ message: "Successfully removed player." });
});
});
// Load node_modules; not needed once something like webpack is getting used
app.get('/node_modules/*', function(req, res) {
res.sendFile(__dirname + req.path);
});
app.get('*', function(req, res) {
res.sendFile(__dirname + "/public/views/index.html"); // load our public/views/index.html file
});
app.listen(port);