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 fs = require('fs'), http = require('http'), mime = require('mime'), url_parser = require('url');
var state = {
started: false,
time: 0,
players: {},
word: null,
tries: [],
who_found: {},
who_draws: null,
drawing: [],
};
var server = http.createServer(function (req, res) {
var url = url_parser.parse(req.url, true);
var filename = 'pictionary.html';
if(url.pathname != '/') filename = '.' + url.pathname;
if(url.pathname == '/get_state') {
res.writeHead(200, {'content-type': 'application/json'});
res.end(JSON.stringify(state));
} else if(url.pathname == '/draw_line') {
state.drawing.push({x1: parseInt(url.query.x1), y1:
parseInt(url.query.y1), x2: parseInt(url.query.x2), y2:
parseInt(url.query.y2)});
res.end();
} else if(fs.existsSync(filename)) {
res.writeHead(200, {'Content-Type': mime.lookup(filename)});
res.end(fs.readFileSync(filename));
} else {
res.writeHead(404, {'content-type': 'text/plain'});
res.end('file not found');
}
});
server.listen(1337);
console.log('server at http://localhost:' + 1337); |
Partager