Home > Blockchain >  When in the browser I typed 'localhost:3005', it works a long time, before display first r
When in the browser I typed 'localhost:3005', it works a long time, before display first r

Time:11-20

I wrote simple server running script on node.js

const http = require('http')

let requestsCount = 0
const server = http.createServer((request, response) => {
    requestsCount  
    response.write(`Leo Garsia ${requestsCount}`)

})

server.listen(3005, () => {
    console.info('Server is Running on port 3005')
})

When in the browser I typed 'localhost:3005', it works a long time, before display first result. (about 10 minutes) Why does it huppens?

And then when I refresh browser it requestsCount increments twice. And displays the result like 2,4,6, and so on. Very interesting why?

CodePudding user response:

When in the browser I typed 'localhost:3005', it works a long time, before display first result. (about 10 minutes)

Your response never finishes, because your code lacks the response.end() statement after the response.write. Therefore the browser waits until timeout (10 minutes) and then displays what it has received so far.

And then when I refresh browser it requestsCount increments twice. And displays the result like 2,4,6, and so on. Very interesting why?

I suspect the other request is the one the browser makes for the favorites icon, see here.

CodePudding user response:

as Heiko TheiBen said, I mess response.end() I have replaced the code with using the 'express'. Now when I type "localhost:3005/leo", the result appears immediately.

const express = require('express')

const app=express();
let requestsCount = 0
app.listen(3005, () =>{
   console.log('Server is running on port 3005...')
})

app.get('/leo', (request, response)=>{
    requestsCount  
    response.write(`Request URL is, ${request.url}  count is, ${requestsCount}`)
    response.end()
    console.info(`Request URL is, ${request.url}  count is, ${requestsCount}`)
})
  • Related