Im trying to write a simple bowling API on a webserver using expressJS along with a client via the terminal. The terminal sends HTTP request to the webserver to be processed and print out its response. I have 3 relatively simple endpoints to start a game, record a frame, and send current scores. The start endpoint works as intended but when trying run the record endpoint I receive a ERR_BAD_RESPONSE
error. Here's the endpoint.
Heres the server code with 3 endpoints:
const express = require('express');
const axios = require('axios');
const app = express();
// Set up a hash map to store the scores for each player
const scores = {};
// Set up the API routes
// Route to start a new game with up to 8 players
app.get('/start', (req, res) => {
// Initialize the scores for each player to 0
for (let i = 1; i <= 8; i++) {
scores[`player${i}`] = 0;
}
res.send(`Started a new game with up to 8 players!`);
});
// Route to record the result of a frame for a player
app.use(express.json());
app.post('/record', (req, res) => {
const player = req.body.data.player;
const score = req.body.data.score;
scores[player] += score;
res.send(`Recorded score of ${score} for ${player}`);
});
// Route to display the current scores for each player
app.get('/scores', (req, res) => {
res.send(scores);
});
// Start the server
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Client code:
const axios = require('axios');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const baseURL = 'http://localhost:3000';
console.log('Welcome to the bowling API!');
async function client(){
let optionSelect = await rl.question("Please select an option:\n1)Start a game\n2)Record a frame\n3)Send scores\n", async (option) => {
if(option === "1"){
await axios.get('http://localhost:3000/start')
.then(response => console.log(response.data))
.catch(error => console.log(error));
client();
} else if(option === "2"){
await axios.post('http://localhost:3000/record', {
data: {
player: 'player1',
score: 10
}
})
.then(response => console.log(response.data))
.catch(error => console.log(error));
client();
} else if(option === "3"){
await axios.get('http://localhost:3000/scores')
.then(response => console.log(response.data))
.catch(error => console.log(error));
client();
} else{
console.log('Bad input selected bye bye');
return;
}});
}
client();
Heres the output:
$ node app.js&
[1] 1050
Chris@DESKTOP-E7JGMQ2 MINGW64 ~/rectangleHealth (main)
$ node otherclient.js
Welcome to the bowling API!
Please select an option:
1)Start a game
2)Record a frame
3)Send scores
1
Started a new game with up to 8 players!
Please select an option:
1)Start a game
2)Record a frame
3)Send scores
2
AxiosError: Request failed with status code 500
at settle (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.c
js:1859:12)
at IncomingMessage.handleStreamEnd (C:\Users\cmb\rectangleHealth\node_module
s\axios\dist\node\axios.cjs:2723:11)
at IncomingMessage.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1359:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:2
1) {
code: 'ERR_BAD_RESPONSE',