In nodejs, how do I check if a port is listening or in use

后端 未结 3 1677
再見小時候
再見小時候 2021-02-19 23:20

I\'ll be very specific here in the hope that folks who understand this can edit to rephrase to the general situation.

Currently when you run "node debug", it sp

3条回答
  •  礼貌的吻别
    2021-02-20 00:15

    Use inner http module:

    const isPortFree = port =>
      new Promise(resolve => {
        const server = require('http')
          .createServer()
          .listen(port, () => {
            server.close()
            resolve(true)
          })
          .on('error', () => {
            resolve(false)
          })
      })
    

提交回复
热议问题