Skip to content

Instantly share code, notes, and snippets.

@yogeshvar
Last active August 14, 2020 12:49
Show Gist options
  • Save yogeshvar/585b8a45170793df24b958c79f6b0309 to your computer and use it in GitHub Desktop.
Save yogeshvar/585b8a45170793df24b958c79f6b0309 to your computer and use it in GitHub Desktop.
tic-tac-toe
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);
});
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