I am learning NodeJS now. I have the following JavaScript code from a project:
$.getJSON('/api/' + entry + '/', function(data) {
if ( data.hasOwnProperty("seq") ) {
$(placement).append( data.seq );
}
});
It theoretically gets an answer from an Express NodeJS API, running at a specific port XXXX. The API can be accessed via localhost:XXXX/api/whateverentry.
The question is: how does this code know which port it should go?
A relative URL reference such as /api that doesn't start with http:// or https:// gets the protocol, port and domain from the host page URL. So, your request will use the port number that the host page used.
This will default to the current port that is being visited in the browser being used because it is a relative URL. If a different host is not specified, then the base of the URL will be used.
For example if you were running this on port 4000 on localhost, then going to http://localhost:4000/api/... is the same as the request above.