http4js

Table of Contents

Uri API

Handy for manipulating a URI. You might want to update just the protocol or get the hostname or port from a URI.

const uri = Uri.of("http://localhost:3000/my/cool/path?tosh=rocks&bosh=pwnage&losh=awsm")
uri.hostname();    // localhost
uri.protocol();    // http
uri.port();        // 3000
uri.queryString(); // tosh=rocks&bosh=pwnage&losh=awsm

And we have methods to give us a new Uri with any of the above changed:

const uri = Uri.of("http://localhost:3000/my/cool/path?tosh=rocks&bosh=pwnage")
uri.withHostname("google").asUriString();    
// http://google:3000/my/cool/path?tosh=rocks&bosh=pwnage 

uri.withProtocol("https").asUriString();   
// https://localhost:3000/my/cool/path?tosh=rocks&bosh=pwnage

uri.withPort(1234);  
// http://localhost:1234/my/cool/path?tosh=rocks&bosh=pwnage

uri.withQuery("losh", "awsm").asUriString(); 
// http://localhost:3000/my/cool/path?tosh=rocks&bosh=pwnage&losh=awsm

uri.withAuth("ben", "ben-password").asUriString();
// http://ben:ben-password@localhost:3000/my/cool/path?tosh=rocks&bosh=pwnage

The full API is as follows

static of(uri: string): Uri 

asUriString(): string 

protocol(): string 

withProtocol(protocol: string): Uri 

queryString(): string 

withQuery(name: string, value: string): Uri 

path(): string

withPath(path: string): Uri 

hostname(): string 

withHostname(hostname: string): Uri 

port(): string 

withPort(port: number): Uri 

auth(): string 

withAuth(username: string, password: string): Uri 

Prev: Request and Response API

Next: Routing API