-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
82 lines (72 loc) · 1.64 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
// Configuration for express app
var express = require('express'),
path = require('path'),
app = express(),
bodyParser = require('body-parser'),
server = require('http').Server(app),
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/grid');
// Mongoose Schema
var Schema = mongoose.Schema;
var ScoreSchema = new Schema({
score: Number,
player: String
}, {
collection: 'score'
});
// Mongoose Model definition
var User = mongoose.model('score', ScoreSchema);
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.set('views', __dirname + '/views/');
app.set('view engine', 'ejs');
// server static file
app.use(express.static(path.join(__dirname, 'public')));
app.post('/score', function(req, res) {
var e = {
score: parseInt(req.body.score, 10),
player: req.body.player
};
var userDoc = new User(e);
userDoc.save(function(err, result) {
if (!err) {
res.json(result);
} else {
res.status(404).send('nothing to see here...');
}
});
});
app.put('/score/:id', function(req, res) {
var e = {
score: req.body.score,
player: req.body.player
};
var userDoc = new User(e);
userDoc.update({
_id: req.params.id
}, function(err) {
if (!err) {
res.json(e);
} else {
res.status(404).send(err);
}
});
});
app.get('/score', function(req, res) {
User.find(function(err, score) {
if (err) {
res.json({
message: 'no result'
});
}
res.json(score);
});
});
app.get('/', function(req, res) {
res.render('index.ejs', {
message: 'coming from server'
});
});
module.exports.http = server;