-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
52 lines (40 loc) · 1.56 KB
/
app.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
// Référence aux dépendances de l'application
var express = require('express');
var path = require('path');
var _ = require('underscore');
var bodyParser = require('body-parser')
var gameController = require(__dirname + '/src/GameController.js');
var bot = require(__dirname + '/src/bot.js');
// Port
var port = process.env.PORT || 3000;
// Démarrage de l'application
var app = express() ;
server = app.listen(port, function() {
console.log('Listening on port %d', server.address().port);
});
// Configuration
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({extended: true}));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
// Routage des requêtes HTTP
app.post('/jeton', function(req, res){
var updateDatedGrid = gameController.updateGrid(req.body.grid, req.body.position, 1);
var isEnd = gameController.isEndGame(req.body.grid, req.body.position, 2);
res.send({grid : updateDatedGrid, end : isEnd});
});
app.post('/opponentMove', function(req, res){
var position = bot.play(req.body.grid);
var updateDatedGrid = gameController.updateGrid(req.body.grid, position, 2);
var isEnd = gameController.isEndGame(req.body.grid, position, 1);
res.send({grid : updateDatedGrid, end : isEnd});
});
app.get('/newGame', function(req, res){
var gameGrid = gameController.newGame();
res.send({grid : gameGrid});
});
app.get('/', function(req, res){
res.render('index', {'title' : 'Puissance 4 - NodeJS + Angular'});
});
module.exports = app;