0

I created a Web Application using AngularJS x ExpressJS where GET "/" of my app.js renders the index.html of the application.

For example, I have GET /test-data on my app.js and trying to fetch those test data from AngularJS via localhost:port/test-data. While this is working on my test environment, I always had to alter the request URL on production to production's REST.

Is there any way that I can prevent the problem stated above editing the URL during deployment?

Sample codes:

(app.js)

var express = require("express");
var app = express();
var request = require("request");
var port = 3000;
var bodyParser = require("body-parser");

app.use(express.static(__dirname));

app.use(bodyParser.json());

app.get("/", function(request, response) {
    response.sendFile(__dirname + "/index.html");
});

app.get("/test-data", function(req, res, next) {
    var returnValue = [
        {
            username : "MAIK",
            fullname : "Michael"
        },
        {
            username : "CLARK",
            fullname : "Clark"
        },
        {
            username : "ROLLY",
            fullname : "Roland"
        },
        {
            username : "FLOYDIE",
            fullname : "Floyd"
        },
        {
            username : "ZOE",
            fullname : "Zoe"
        }
    ];

    res.send(returnValue);

});

app.listen(port, function() {
    console.log("Listening to port " + port + "...");
});

(MySampleService.js)

.service("MySampleService", function($http) {

    this.sampleURL = "localhost:3000";    

    this.getTestData= function(){
        return $http
        (
            {
                method : "GET",
                url : "http://" + this.sampleURL + "/test-data
            }
        );
    };
});
4
  • You are hardcoding the url in your service here, ` this.sampleURL = "localhost:3000";` Commented Mar 6, 2017 at 9:50
  • @SudhansuChoudhary Is there any way that I can send an HTTP request declaring the host URL? Commented Mar 6, 2017 at 9:54
  • have a switch condition for your dev, test and prod environments which returns the host name for those three envs. Based on the env value that you get from the switch condition, you would stitch the host url or any of the urls needed in your service. However you can use "location.hostname" to get the host name. Commented Mar 6, 2017 at 10:02
  • the first condition is an ideal way of doing things, but the second one works too nevertheless Commented Mar 6, 2017 at 10:03

1 Answer 1

2

For your URL, simply use /test-data. The request automatically knows to use the same port the app is hosted on.

Sign up to request clarification or add additional context in comments.

1 Comment

It does! I tried $location.host() + ":" + $location.port() after reading the comments. But I think this is the simplest way to deal with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.