Koa: Get port koa is running on

Created on 20 Jan 2018  路  2Comments  路  Source: koajs/koa

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.

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 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;

All 2 comments

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;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ke1Del picture ke1Del  路  3Comments

sibelius picture sibelius  路  3Comments

rally25rs picture rally25rs  路  4Comments

tracker1 picture tracker1  路  3Comments

ElegantScripting picture ElegantScripting  路  5Comments