Skip to main content

Posts

Showing posts with the label Get

Express get request code: 'ERR_HTTP_HEADERS_SENT' when sending response

So this is a common problem in Express, this question is asked a lot but none of them worked for me, I have a get request with axios to my express server to check the auth status of the user with firebase, everything works fine, but this error shows in my console, here's the code: Server.js const firebaseDb=require("firebase/database"); const firebaseAuth=require("firebase/auth"); const path = require('path'); const express= require("express"); var cors = require('cors'); const bodyParser = require("body-parser"); const app=express(); app.use(express.json()); app.use(cors()); app.use(bodyParser.urlencoded({extended: true})); const db=firebaseDb.getDatabase(); const auth=firebaseAuth.getAuth(); app.get('/checkAuth',(req, res)=>{ firebaseAuth.onAuthStateChanged(auth, (user) => { if (user) { // User is signed in. return res.status(200).send("auth ok"); } else { // User i...

Express not allowing more than one pending request from the same IP?

I'm making a chat application that uses long polling with express (I'm aware websockets are better for this, I just wanted to make something specifically with long polling). This is the code that the client sends a GET request to when it's waiting for a new chat message: app.get('/api',(req,res)=>{ listeners.push(res) }) It pushes the response objects to an array, and once a new chat message is available it calls .send() on the responses. When the client recieves the response, it sends a GET request again, waiting for new messages. Here's the code for when someone sends a message (simplified) app.post('/api', (req,res)=>{ listeners.forEach(listener=>{ listener.json(req.body) }) listeners = [] res.status(200).end() }) I noticed that when I open two tabs of my site, only the 1st tab's request gets put in the array at first. That makes the 2nd tab not receive the 1st chat message. The 2nd tab's reque...