4.7 Encoding User Input

Special characters need to be handled so that GEO API could properly recognize the address from user input. For example, the blank space should be swapped with %20.

We are going to learn how to encode and decode the URL string so that the address can be dynamic from user input.

Before we encode and decode, let’s set up the yargs command line interface.

const argv = yargs
.options({
a: {
demand: true,
alias: 'address',
describe: 'Address to fetch weather for',
string: true
}
})
.help()
.alias('help', 'h')
.argv;

This setting is a little different from last time because our weather app is not requiring a lot of arguments, but options will do.

string: true chooses to get a string argument after the option a, and get an empty string if nothing is fed in.

Key Functions

Modify URL to Support Custom

url: `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(argv.a)}``