Currently, the interfaces defined in the module won't show this, but after a bit of console logging, I see that when running your code:
let listener = app.listen(randomPort, function(){
console.log('Listening on port ' + listener.address().port);
});
the value of listener.listener.addr
is an object like this:
{ hostname: "0.0.0.0", port: 8000, transport: "tcp" }
Unfortunately, since this is not explicitly declared in the type, you'll get a linting error if you're using TypeScript. We can hack around this with a bit of type coercion:
const currentPort: number = (listener.listener.addr as { port: number }).port
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…