I created a web server that acts as a client-side application using socket.io-client and express. This setup is necessary for another project I am working on.
The web server emits the posted string and responds by sending the served string when it receives 'boom' emit from the io server.
Posting 'heat_bomb' works fine the first time, but when attempting a second time, an error '[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client' occurs at res.send(data) in socket.on().
Is there a way to refresh every time a post request is generated so that each request has an independent response?
app.ts
import express from 'express'
import {io} from 'socket.io-client'
import bodyParser from 'body-parser'
const app=express()
const PORT=8080
const socket=io(`http://localhost:2002`, {
query:{
hello:"merhaba"
}
})
app.use(bodyParser.urlencoded({extended:false}))
app.get('/', (req, res)=>{
res.sendFile(__dirname+`/index.html`)
})
app.post('/heat_bomb', (req, res)=>{
socket.emit('heat_bomb', req.body.elem)
socket.on('boom', (data)=>{
res.send(data)
})
})
app.listen(PORT, ()=>{
console.log(`Server Running: ${PORT}`)
})
index.html
$('#heat_button').click(function(){
console.log('heating bomb')
$.post('/heat_bomb', {elem: $('#input_number').val()},(data, status)=>{
console.log(data)
console.log('heated')
})
})