This repository has been archived by the owner on Mar 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (62 loc) · 1.94 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
var path = require('path');
var express = require('express');
var game = require(path.join(__dirname, 'game'));
var network = require(path.join(__dirname, 'network'));
var createServer = function(port) {
var app = express();
/**
* Game state
*
* Responds with JSON object representing current game state
*/
app.get('/api/v1/game', function(req, res) {
game.state.get(function(err, state) {
// if there is an error, see if it is one we can handle
if (err) {
console.log('there was an error! ' + err);
if (/state does not exist/.test(err)) {
// if the state does not exist, create it
game.state.create(function(err, state) {
if (err) return res.status(500).json({ error: err });
//console.log(state);
res.json(state);
});
} else {
// unknown errors get sent in response
return res.status(500).json({ error: err });
}
} else {
// no errors, so respond with game state
console.log('there was no error.')
res.json(state);
}
});
});
/**
* Network state
*
* Responds with JSON object representing the current network state
*/
app.get('/api/v1/network', function(req, res) {
network.state.read(function(err, state) {
if (err) {
// if there is an error, see if it is one we can handle
if (/state does not exist/.test(err)) {
// if the state does not exist, create it
network.state.create(function(err, state) {
if (err) return res.status(500).json({ error: err });
res.json(state);
});
} else {
// unknown errors get sent in response
return res.status(500).json({ error: err });
}
} else {
console.log('no errors getting network state');
res.json(state);
}
});
});
return app.listen(port);
};
module.exports = createServer;