node.js - node端口占用要怎么處理?
問題描述
node端口占用要怎么處理
問題解答
回答1:在www里面改一下監聽端口就好。
回答2:如果能換端口,找到你項目中config.js 看到那個port了嗎?改后面的值就行。如果不能換端口的話。。
圖是在windows下的,那就按windows的方法了:
netstat –ano|findstr '8080'
找到對應進程的PID,然后:
taskkill -PID <進程號> -F
或者進任務管理器,找到對應PID的進程,結束之。
回答3:這種情況,十有八九是你的這個程序已經啟動或者你的另一個node文件正在監聽這個端口。只需要將那個程序應用ctrl+c掉。
回答4:殺掉進程,或者啟動更換端口
回答5:https://segmentfault.com/a/11...
Node 中實現端口被占用了,使用另外一個端口為了解決 ssr 工具 起多個服務的時候端口被占用的情況。分享研究的代碼片段。
// 檢測port是否被占用function probe(port, callback) { var server = net.createServer().listen(port) var calledOnce = false var timeoutRef = setTimeout(function () {calledOnce = truecallback(false,port) }, 2000) timeoutRef.unref() var connected = false server.on(’listening’, function() {clearTimeout(timeoutRef)if (server) server.close()if (!calledOnce) { calledOnce = true callback(true,port)} }) server.on(’error’, function(err) {clearTimeout(timeoutRef)var result = trueif (err.code === ’EADDRINUSE’) result = falseif (!calledOnce) { calledOnce = true callback(result,port)} })}
使用例子:
function server(_port){ var pt = _port || __port; probe(pt,function(bl,_pt){// 端口被占用 bl 返回false// _pt:傳入的端口號if(bl === true){ // ssr(_pt) server = http.createServer(connListener); server = server.listen(parseInt(_pt, 10)); console.log('n Static file server running at' + 'nn=> http://localhost:' + _pt + ’n’);}else{ server(_pt+1)} })}回答6:
第一步:運行lsof -i:端口號。通過這個命令你可以看到占用端口號的進程ID。第二步:kill 進程ID即可。
X,你是windows啊。。。當我沒說。
相關文章:
1. angular.js - 關于$apply()2. angular.js使用$resource服務把數據存入mongodb的問題。3. MySQL數據庫中文亂碼的原因4. dockerfile - 我用docker build的時候出現下邊問題 麻煩幫我看一下5. nignx - docker內nginx 80端口被占用6. android-studio - Android Studio 運行項目的時候一堆警告,跑步起來???7. dockerfile - [docker build image失敗- npm install]8. mysql - 新浪微博中的關注功能是如何設計表結構的?10. 如何解決Centos下Docker服務啟動無響應,且輸入docker命令無響應?
