AngularJS请求通过Localhost expressjs



我使用 app.js的 CC_1的get"/"创建了一个Web应用程序。

例如,我在app.js上获得了/test-data,并尝试通过localhost:port/test-data从AngularJS获取这些测试数据。在我的测试环境上工作时,我总是必须将生产中的请求URL更改为生产的休息。

有什么办法可以防止在部署过程中编辑URL上方所述的问题?

示例代码:

(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
            }
        );
    };
});

对于您的URL,只需使用/test-data即可。请求自动知道使用该应用程序托管的同一端口。

最新更新