V
V
Volodymyr Palamar2021-07-19 12:34:15
Node.js
Volodymyr Palamar, 2021-07-19 12:34:15

How to fix magical behavior of http module in heroku?

Hello, I am writing a viber bot and there is a problem with the http module, sometimes it just refuses to accept a webhook, it only happens on heroku locally, everything is ok...
But if you use express, the problem disappears... Who knows why this happens?

http code
const server = http.createServer((req,res)=>{
    res.end("{\"status\":\"ok\"}") // End requests
    let rdata = '' // Raw data:JSON
    if (req.method == "POST"){ // is request == post
        req.on("data",chank=>{
            rdata+=chank      // on chank => data += chank 
        })

        req.on("end",e=>{ // on data end
            let data = {} // clear data
            try {
                data = JSON.parse(rdata) //try parse raw data
                console.log(`recive: ${rdata}`)
            } catch (error) {
                console.error(error)
            }
            if(data){
                new ViberBot(data).do() 
            }

        }) 
    }
})

server.listen(process.env.PORT || 8080)

express code
const app = express()
//Start server

app.post("/",(req,res)=>{
    res.sendStatus(200);
    let rdata = '' // Raw data:JSON
    // is request == post
    req.on("data",chank=>{
        rdata+=chank      // on chank => data += chank 
    })

    req.on("end",e=>{ // on data end
        let data = {} // clear data
        try {
            data = JSON.parse(rdata) //try parse raw data
            console.log(`recive: ${rdata}`)
        } catch (error) {
            console.error(error)
        }
        if(data){
            new ViberBot(data).do() 
        }

    }) 
    
})
app.listen(process.env.PORT || 8080, () => {
    console.log(`Example app listening`)
  })

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question