我在 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')
})
當我在瀏覽器中輸入“localhost:3005”時,它運行了很長時間,然后才顯示第一個結果。(大約 10 分鐘)為什么會打嗝?
然后當我重繪 瀏覽器時,它 requestsCount 增加了兩次。并顯示 2、4、6 等結果。很有趣為什么?
uj5u.com熱心網友回復:
當我在瀏覽器中輸入“localhost:3005”時,它運行了很長時間,然后才顯示第一個結果。(約10分鐘)
您的回應永遠不會完成,因為您的代碼缺少.response.end()之后的陳述句response.write。因此,瀏覽器會等到超時(10 分鐘),然后顯示到目前為止收到的內容。
然后當我重繪 瀏覽器時,它 requestsCount 增加了兩次。并顯示 2、4、6 等結果。很有趣為什么?
我懷疑另一個請求是瀏覽器對收藏夾圖示發出的請求,請參見此處。
uj5u.com熱心網友回復:
正如 Heiko TheiBen 所說,我把 response.end() 搞得一團糟,我已經用“快遞”替換了代碼。現在,當我輸入“localhost:3005/leo”時,結果會立即出現。
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}`)
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/537795.html
標籤:节点.js网址
上一篇:如何從請求陣列中獲取資料?
