Is there a koa equivalent of server.address().port in express that can be used to print out the port that koa is running on? If not would be nice to have available.
const Koa = require('koa')
const app = new Koa()
const server = app.listen(3000)
console.log(server.address().port)
// -> 3000
I ran across this in a search and figured I would share my knowledge.
If you are using Typescript, you must take an extra step to cast the address function to the correct type before you can access the port. Otherwise you get errors about port not existing.
import { AddressInfo } from 'net';
const port = (<AddressInfo>server.address()).port;
Most helpful comment
I ran across this in a search and figured I would share my knowledge.
If you are using Typescript, you must take an extra step to cast the
addressfunction to the correct type before you can access theport. Otherwise you get errors aboutportnot existing.