Last active
August 14, 2020 12:49
-
-
Save yogeshvar/585b8a45170793df24b958c79f6b0309 to your computer and use it in GitHub Desktop.
tic-tac-toe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch(`http://localhost:3000`) | |
.then(result => result.json()) | |
.then(response => console.log(response)) | |
async function postData(url, data) { | |
const response = await fetch(url, { | |
method: 'POST', | |
body: JSON.stringify(data) | |
}); | |
return response.json(); | |
} | |
postData(`http://localhost:3000`, { player: 1, position: 2 }) | |
.then(data => { | |
console.log(data); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const bodyParser = require('body-parser'); | |
const app = express() | |
const port = 3000 | |
let gamePositions = []; | |
let player1 = []; | |
let player2 = []; | |
app.use(bodyParser.json()); | |
app.get('/', (req, res) => { | |
var response = { player1: player1, player2: player2 } | |
res.send(response); | |
}) | |
app.post('/game', (req, res) => { | |
console.log(`saving player's move`); | |
if (req.body.player == 1) { | |
console.log('player1 move'); | |
player1.push(req.body.position); | |
} else { | |
console.log('player2 move'); | |
player2.push(req.body.position); | |
} | |
res.send({ player1: player1, player2: player2, success: true, message: 'saved position' }); | |
}) | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment