-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (34 loc) · 1.21 KB
/
index.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
var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var bodyParser = require('body-parser');
var assert = require('assert');
var app = express();
var url = 'mongodb://generatorr:[email protected]:13915/score';
var port = process.env.PORT || 8080;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', function (req, res) {
MongoClient.connect(url, function(err, database) {
assert.equal(null, err);
database.collection('gameScore').find().toArray(function(err, response) {
return res.send(response);
});
database.close();
});
});
app.post('/', function (req, res) {
console.log('request --->', req);
console.log('request BODY --->', req.body);
MongoClient.connect(url, function(err, database) {
var collection = database.collection('gameScore');
assert.equal(null, err);
collection.insert({
name: req.body.name || 'Unknown player',
score: req.body.score || 'no score'
});
res.send(200);
});
});
var server = app.listen(port, function () {
console.log('Server running at http://localhost:' + server.address().port)
});