繁体   English   中英

在本地主机上运行但不在heroku上运行的angularjs应用

[英]angularjs app running on localhost but not on heroku

我的angularjs应用在我的机器上的localhost上运行良好。 但是,当我将其部署到heroku时,该应用程序正在显示,但是其中一些功能(主要是前端到后端的通信)失败了。 我怀疑这是因为我使用了http://localhost:3000而不是我的真实网站addess: https://mytestapp.herokuapp.com ://mytestapp.herokuapp.com。 有人可以让我知道如何在Heroku上运行该应用程序吗? 后端与前端进行通信以使登录/注册功能运行。 此刻,当我登录或注册时,我得到一个错误,好像什么都没发生。

我运行grunt build ,并从.gitignore删除了/dist ,并拥有一个类似于以下web: node api.jsprocfile web: node api.js

和我的package.json文件是这样的:

  "version": "1.0.0",
  "main": "Gruntfile.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "start": "nf start",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

我还在app.config.js中声明了一个常量,例如:

  .constant('API_URL', 'http://localhost:3000/')

我使用API_URL设置我的登录/注册身份验证提供者。

在我的gruntfile.js中,我也有以下几点:

connect: {
  options: {
    port: 9000,
    // Change this to '0.0.0.0' to access the server from outside.
    hostname: 'localhost',
    livereload: 35729
  },
  livereload: {
    options: {
      open: true,
      middleware: function (connect) {
        return [
          connect.static('.tmp'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect().use(
            '/app/styles',
            connect.static('./app/styles')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },
  test: {
    options: {
      port: 9001,
      middleware: function (connect) {
        return [
          connect.static('.tmp'),
          connect.static('test'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },

更新

我想实现它在这个控制器,但没有成功:当我落实号Yoni的解决方案,我没有得到projectlist了,它显示了一个空白页。 这是为什么?

'use strict';
angular.module('ModuleV11App')
  .controller('ProjectlistCtrl', function ($http, $scope, API_URL, alert) {
    return $http.get($rootScope.server.url + '/projectlist').success(function (projectlist) {
      $scope.projectlist = projectlist;
    }).error(function (err) {
      alert('warning', "Unable to get projects, please login first", err.message);
    })

  });

但是,它的工作原理如下:

return $http.get(API_URL + 'projectlist').success(function (projectlist) {

使用$ location服务

您可以执行以下操作:

angular.module('mainModule', [])
    .run(function($location,$rootScope) {
        $rootScope.server = {url: location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '')};
    })

然后,您可以在前端代码中的任何位置进行这样的调用:

$http.get($rootScope.server.url + '/whatever');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM