Home > other >  express cant handle too many requests?
express cant handle too many requests?

Time:04-24

Im currently working on my new MERN stack project. Im trying to get information about time spent on every location and everything seemed ok until i' ve discovered a bug. After changing location and sending post request to server about 10 times whole localhost server is frozing. Any calls to the server stop working. This is very frustrating and i cant figure it out. Do you guys have any ideas?

UseEffect calling action:

    useEffect(() => {

     dispatch(savePageStats())
}, [location])

Redux action call:

export const savePageStats = () => async (dispatch) => {
try{
    const arr = []
    await api.savePageSession(arr)    
}catch(err){
    console.log(err)
}

Axios api:

export const savePageSession = (arr) => API.post('/stats/savepagesession', arr)

Express router:

    const app = express()
app.use(bodyParser.json({ limit: '30mb', extended: true}))
app.use(bodyParser.urlencoded({ limit: '30mb', extended: true}))
app.use(cors())
app.use('/users', usersRoutes)
app.use('/messages', messagesRoutes)
app.use('/stats', statsRouter)

dotenv.config()
mongoose.connect(process.env.CONNECTION_URL)
.then(() => app.listen(process.env.PORT, () => console.log(`server running on port ${process.env.PORT}`) ))
.catch((err) => console.log(err))

Express controler

export const savePageSession = async (req, res) => {
try{
   
    console.log('im here')
    
}catch(err){
    res.status(500).json({message: 'something went wrong'})
}

}

CodePudding user response:

The savePageSession route handler doesn't send any response back to the client. That means the browser will still be waiting for a response and will eventually stop sending requests to your server until the previous requests finish.

Add at least something simple res.send("ok"); to the route. All http request handlers on your server MUST send some kind of response back to the client.

  • Related