Re-assembling the Requested URL

It is simple to know the request URL path, it’s one (this.url in Koa) used by routing, but if you are looking to get the complete URL, it’s a little bit complicated. Luckily, there is a quick way to re-assemble the requested URL in Koa, if proxy is not a concern:

var url = this.request.protocol + '://' + + this.request.originalUrl;

Or a shorter version:

var url = this.protocol + '://' + + this.originalUrl;

The port number is included in, for example: localhost:3000.

Putting into a complete script:

require('koa')().use(function *() {
// Echo back the request URL.
this.body = this.protocol + '://' + + this.originalUrl;

Assume that you have a server running on the host with SSL enabled. If you make a request to the server with the URL:, this URL will be echoed back in the response by the above script.