search

Home  >  Q&A  >  body text

node.js - node端口占用要怎么处理?

node端口占用要怎么处理

巴扎黑巴扎黑2863 days ago817

reply all(6)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 14:55:58

    Just change the listening port in www.

    reply
    0
  • 迷茫

    迷茫2017-04-17 14:55:58

    If you can change the port, find the config.js in your project. Do you see that port? Just change the latter value.
    If you cannot change the port. .

    The picture is under windows, so follow the windows method:

    netstat –ano|findstr "8080"

    Find the PID of the corresponding process, then:

    taskkill -PID <进程号> -F

    Or go into the task manager, find the process corresponding to the PID, and end it.

    reply
    0
  • 阿神

    阿神2017-04-17 14:55:58

    In this case, nine times out of ten, your program has already been started or another node file is listening to this port. Just apply that program ctrl+c away.

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:55:58

    Kill the process, or start changing the port

    reply
    0
  • 阿神

    阿神2017-04-17 14:55:58

    https://segmentfault.com/a/11...

    The implementation port in Node is occupied, use another port

    In order to solve the problem that the port is occupied when the ssr tool starts multiple services. Share code snippets from your research.

    // 检测port是否被占用
    function probe(port, callback) {
    
        var server = net.createServer().listen(port)
    
        var calledOnce = false
    
        var timeoutRef = setTimeout(function () {
            calledOnce = true
            callback(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 = true
            if (err.code === 'EADDRINUSE')
                result = false
    
            if (!calledOnce) {
                calledOnce = true
                callback(result,port)
            }
        })
    }
    

    Usage example:

    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" + "\n\n=> http://localhost:" + _pt + '\n');
            }else{
                server(_pt+1)
            }
        })
    }

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:55:58

    Step 1: Run lsof -i:端口号. With this command you can see the process ID occupying the port number.
    Step 2: kill 进程ID That’s it.

    X, you are windows. . . When I didn't say it.

    reply
    0
  • Cancelreply