My latest project involves building a Web Application using AngularJS and ExpressJS. In this application, I have set up a GET "/" route in my app.js file to render the index.html page when accessed.
One interesting feature is the GET "/test-data" route in my app.js file, which allows me to fetch test data from AngularJS using "localhost:port/test-data". While everything works perfectly in my test environment, I often find myself updating the request URL for production's REST endpoints whenever I deploy the application.
I am looking for a solution that will eliminate the need to manually edit the URL during deployment. Is there a way to automate this process?
Below are snippets of code from my project:
(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
}
);
};
});