-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
63 lines (60 loc) · 1.48 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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/players');
var Player = mongoose.model('Player', mongoose.Schema({
name:String,
joiningdate:Date,
birthdate:Date,
type:String,
contact:String
}));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/client'));
app.get('/api/players', function(req, res){
Player.find(function(err, players){
if(err)
res.send(err);
res.json(players);
});
});
app.get('/api/players/:id', function(req, res){
Player.findOne({_id:req.params.id}, function(err, players){
if(err)
res.send(err);
res.json(players);
});
});
app.post('/api/players', function(req, res){
Player.create( req.body, function(err, players){
if(err)
res.send(err);
res.json(players);
});
});
app.delete('/api/players/:id', function(req, res){
Player.findOneAndRemove({_id:req.params.id}, function(err, players){
if(err)
res.send(err);
res.json(players);
});
});
app.put('/api/players/:id', function(req, res){
var query = {
name:req.body.name,
joiningdate:req.body.joiningdate,
birthdate:req.body.birthdate,
type:req.body.type,
contact:req.body.contact
};
Player.findOneAndUpdate({_id:req.params.id}, query, function(err, players){
if(err)
res.send(err);
res.json(players);
});
});
app.listen(3000, function(){
console.log('server is running on port 3000..');
});