Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (67 loc) · 1.62 KB
/
Copy pathapp.js
File metadata and controls
84 lines (67 loc) · 1.62 KB
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
/**
* Module dependencies.
*/
require.paths.unshift(__dirname);
var express = require('express')
, board = require('board')
, fs = require('fs');
var app = module.exports = express.createServer();
// Configuration
app.configure(function() {
app.use(express.bodyDecoder());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.staticProvider(__dirname + '/public'));
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function() {
app.use(express.errorHandler());
});
// Load Board function
function load_board(callback) {
fs.readFile('board.hash', function(err, data) {
if (err) {
console.log("Missing");
board.pieces = {};
}
else {
board.pieces = JSON.parse(data);
}
callback();
})
}
// Routes
app.get('/reset', function(req, res) {
console.log("Reset");
board.create();
res.send(board.pieces);
});
app.get('/', function(req, res) {
console.log("Root");
load_board(function () {
res.send(board.pieces);
});
});
app.get('/show/:pos', function(req, res) {
console.log("Show");
load_board(function () {
pos = req.params.pos
ret = {}
ret[pos] = board.pieces[pos]
res.send(ret)
});
});
app.get('/move/:from/:to', function(req, res) {
console.log("Move");
load_board(function () {
res.send({removed: board.move(req.params.from, req.params.to)})
board.save();
})
})
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port)
}