I have written the following function in Node.js in order to get json data from another server, with a view to saving it in a database and doing further processing. The current function is:
const db = require('../models/db.js')
const gameHistDB = db.gameHistDB
const gameControlDB = db.gameControlDB
const http = require('http')
const playMove = async (req, res) => {
try{
console.log("playing")
var playOptions = {
hostname:'115.146.93.216',
port: 5000,
path: '/action/a159257a6840135d2edd5a3de3017356/game1/46',
method: 'GET',
agent:false,
Connection:'close'
}
console.log(playOptions.path)
var returnedData;
var req = http.request(playOptions, res => {
console.log(res.statusCode)
let data = ''
res.on('data',(d) => {
data += d;
console.log(d)
});
res.on('close',()=>{
returnedData = JSON.parse(data)
console.log(returnedData.turninfo.gamestep)
// gameHistDB.insert(returnedData)
});
})
req.on('error',error => {
console.error(error)
})
req.end()
console.log('Note that we got here')
} catch (err) {
console.log(err)
}
}
(this is possibly a little more complex than it needs to be, but I'm trying to see exactly what is happening at what point)
When I invoke this the browser hangs in "getting data" mode and doesn't seem to get to a state where it thinks all the data has been processed, although it is definitely going into the 'on close' block and logging the right data. If I uncomment the code which is inserting the requested json into the database then very nasty stuff happens, with constant nano errors telling me to revalidate the cache.
I'm also unsure whether it's good workflow to do the database queries within res.on('close') - in other functions where I'm dealing with db queries I'm using await to ensure the query completes before doing other stuff, but it seems like I can't do that here.
Any help appreciated
EDIT: As in comments, I think maybe it is the json sending code on the other machine which is at fault. That is:
try{
console.log("playing")
...
const python = spawn('python3',['./playGameMove.py',JSON.stringify(gamestep),req.params.move]);
python.stdout.on('data',function(data){
console.log('Getting data from playGameMove.py');
nextState.push(data);
});
python.on('close',(code) => {
console.log('in close');
res.send(nextState.join(""))
});
}catch(err){
console.log(err)
}
Is there something I should do after res.send to ensure this code knows that it's done?