node启动服务器自动找本地未被使用的端口号

阅读 62

2022-07-13


使用portfinder模块查找端口号

代码示例

const portfinder = require('portfinder')

portfinder.basePort = PORT

portfinder.getPort((err,) => {
if (err) {
console.error(err);
}
const url = `http://localhost:${port}/path/`;
console.log('> Listening at ' + url + '\n');

const app = server.listen(port, '0.0.0.0', () => {
console.log(app.address().address);
console.log(app.address().port);
});
});


精彩评论(0)

0 0 举报