Obtaining the dynamic address of a web server through JavaScript

In my Angular JS application, the backend is developed using .NET. To retrieve data, I make calls to the API controller methods from the .NET project.

When calling the controller methods, I have to prepend "" to the path of the method. Therefore, the complete URL looks like this:

http://localhost:port/api/controller/method

While this process seems straightforward, when transitioning the app to higher environments, I find myself manually updating this string with the respective server IPs. This means the same URL on a production environment becomes:

http://PROD-IP:port/api/controller/method

Despite the logic behind this approach, I have a couple of queries:

  1. Shouldn't localhost automatically point to the server? If I deploy my app on 127.0.0.1 in every environment, then shouldn't localhost:port work seamlessly each time without needing to modify the IP for different environments? (This is my assumption and unfortunately, it is not effective)

  2. Is there a way in JavaScript to extract the IP address from the current URL? This would allow users to enter the app URL initially, and then use the extracted server URL for all backend calls later on. I attempted to use document.location, but it did not yield the expected results.

I searched for solutions on several blogs, yet I couldn't quite articulate my issue properly and consequently, I didn't come across any suitable solutions.

Answer №1

In the realm of JavaScript, there is no built-in capability to work with hostnames or IP addresses directly. To translate a hostname to an IP address, you will need to employ an external service for assistance. One approach involves extracting the domain name via location.hostname, followed by leveraging third-party web services to identify the corresponding IP address.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Cannot instantiate Marker Clusterer as a constructor

I am facing an issue with implementing Marker Clusterer in my app. I have successfully installed '@google/markerclusterer' in my project and imported it as shown below. However, a puzzling error keeps popping up: core.js:4002 ERROR TypeError: _go ...

Creating a 2D Image Display in three.js

I'm facing a challenge with my threejs project. My goal is to have a 2D image appear on the screen when I press a key. I've done some research but haven't been able to find a solution that works for me. The methods I've tried either don ...

Enhancing the visual appeal of a javascript canvas animation

I am facing a small issue and I want to incorporate an animation from: http://codepen.io/chuckeles/pen/mJeaNJ My main query is how can I modify it so that instead of dots, there would be small images? Which part of the code should I edit for this purpose? ...

Unveiling the Mystery: How Browser Thwarts Server Push in HTTP/2.0

After studying the documentation of HTTP/2.0, I discovered that it is feasible to completely close a referenced stream using the RST_STREAM frame. Check it out here: ()! I am curious about how this feature can be implemented in popular web browsers such a ...

Error loading console due to JSON data on website

After creating a Json data file named question.json with the following content: "Endocrinology":[ { "title":"Endocrinology", "id": "001", "date":"08J", "question":"In adult men, anterior pituitary insufficiency does not caus ...

What is the process of integrating data retrieved from an SQL query into a Pug template using Express and MySQL?

Currently, I am in the process of developing a basic web application that will initially show a list of bus route numbers and names upon landing on the page. My tech stack includes MySQL integrated with Express and Pug. Below is the server-side code snippe ...

Ways to minimize an array using group by

I have a large dataset that needs to be grouped by COAGrpCode and ldgrGrp. Specifically, I need to sum the values of Opening, PrdDr, PrdCr, and Closing for each unique combination of COAGrpCode and ldgrGrp. Below is a sample of the data, which consists of ...

Transferring the mistakes to the final return statement

I am currently working on a script that checks if a username is available or already taken. When the username is found to be taken, I need to assign an error message to my errors object. However, I am struggling with passing these errors from the inner if ...

Tips for inserting a row component into a table using Angular 7

I am currently using the latest version of Angular (7.2.0). I have created a custom tr component as follows: import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-table-row', templateUrl: './table- ...

Executing a PUT XMLHttpRequest request results in an empty response being logged to the

I've been struggling to send a PUT request using XMLHttpRequest, but I keep getting an empty response. I'm using JS for the front end and nodejs for the backend. Even though the network section in the dev tools shows that the PUT request is okay, ...

Retrieve the screen dimensions, including the source code panel and debug panel, utilizing jQuery

$(window).height(); $(window).width(); I am currently utilizing these JavaScript codes to obtain the screen size. However, I have noticed that when the source code panel/debug panel is open, it affects the accuracy of the screen size results. Is there a w ...

What is the process for invoking an MVC Api controller's Post action with a HttpResponseMessage using JavaScript as the client

Currently, I am making an API call from the client side. Below is the code snippet for both the client and server sides: API Action in C#: public class StudentTestController : ApiController { [HttpPost] public HttpResponseMessage GetL ...

Transferring a JavaScript variable to C# to execute an SQL SELECT query, then sending the returned result back to JavaScript

I am facing an issue while trying to execute code in my Code Behind to query my SQL Server using a JavaScript variable and then return the result as an Integer back to my Javascript. My approach involves running some Javascript code initially to obtain a ...

Instructions for setting up a "beta edition" with npm or bower

Is there a way to download Dojo version 1.11.0-pre using npm or bower? I have tried adding the dependency in my package.json file, but npm is unable to locate it. Any suggestions on how to resolve this issue and successfully load Dojo 1.11.0-pre? { " ...

How to retrieve ancestor route parameters in Angular?

My route structure is as follows: const appRoutes:Routes = [ { path: 'article', component: ArticleComponent, children: [ { path: '', component: ArticleListComponent }, { path: ...

Deactivate fields when marked as checked

I have been attempting to deactivate 2 select fields within a fieldset when a checkbox is checked. I have tried methods such as using getElementById, getElementsByTagName, and .attr() but unfortunately, none of them seem to work. This is what I am aiming ...

Interpreting an undefined HTTP GET request within a Node.js server

I am encountering an issue within my Node.js application. When I send an http get request through an ajax call on the client-side, the server-side does not recognize the request data and returns an "undefined" error message. This problem is puzzling to me ...

Having trouble with the "npm install" command after updating my dependencies

While working through Google's Angular tutorial, I encountered some issues after completing step 7. In order to address the problem, I made adjustments to my dependencies in bower.json. Here is what I included ("angular-route": "~1.4.0") : { "name ...

The Bootstrap modal will not appear when the parent container's position is fixed

I am having an issue with a bootstrap modal that I am using as an instruction guide. My goal is to keep it fixed at the top-right corner of the viewport, so I tried using the fixed position. However, when I do this, the modal turns grey. On the other han ...

Tips for adding a gradient to your design instead of a plain solid color

I stumbled upon a snippet on CSS Tricks Attempting to replace the green color with a gradient value, but unfortunately, the value is not being applied. I have tried using both the fill property and gradient color, but neither has been successful. Here is ...