如何使用Node.js在Heroku中托管AngularJS应用程序而不使用yeoman?

pga*_*mou 11 heroku node.js express angularjs angularjs-ng-route

我正在尝试使用Node.js将AngularJS的Hello World构建推送到Heroku中.但有多个视图(部分).

我首先在不使用ngRoute的情况下部署了Hello World,这意味着:没有部分.那很好,很顺利.然后,我尝试推送2个简单的部分.但我认为问题是托管应用程序,同时要求部分.我知道这不是正确的方法,我想要你的建议.

这是我的index.html:

<!DOCTYPE html>
<html ng-app="main">
<head>
    <meta name="name" content="something">
    <title></title>
</head>
<body>
    <section ng-view=""></section>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.min.js" type="text/javascript" charset="utf-8"></script>

    <script type="text/javascript" charset="utf-8">
    angular.module('main', ['ngRoute'])
    .config(['$routeProvider', '$http', function ($routeProvider, $http){
        console.log('hola');
        $routeProvider
        .when('/', {
            resolve: **??? I tried with templateUrl but did not work either**
            ,controller: 'indexCtrl'
        })
        .when('/second', {
            resolve: **???**
            ,controller: 'secondCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
    }])
    .controller('indexCtrl', ['$scope', function ($scope){
        $scope.helloWorld = "Hello World";
    }])
    .controller('secondCtrl', ['$scope', function ($scope){
        $scope.helloWorld = "World Hello";
    }]);
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

部分'templates/second.html'

<h1>{{ helloWorld }}<h1>
<a href="#/" title="">Go to First</a>
Run Code Online (Sandbox Code Playgroud)

部分'templates/index.html'

<h1>{{ helloWorld }}<h1>
<a href="#/second" title="">Go to Second</a>
Run Code Online (Sandbox Code Playgroud)

我的快递应用:

var express = require('express'),
app = express();
app.set('view engine', 'html');
app.get('/', function(req, res) {
    res.sendfile('index.html', {root: __dirname })
});
app.get('/index', function (req, res){
    res.sendfile('templates/index.html', {root: __dirname })
});
app.get('/second', function (req, res){
    res.sendfile('templates/second.html', {root: __dirname })
});
var server = app.listen(process.env.PORT || 80);
Run Code Online (Sandbox Code Playgroud)

显然,Procfile:

web: node index.js
Run Code Online (Sandbox Code Playgroud)

到目前为止我发现的所有教程都使用Yeoman,但我不想使用Yeoman或任何其他脚手架(如果那就是那个)工具.

问:是否可以在我存储部分的Heroku应用程序中托管AngularJS应用程序?如果是这样,我做错了什么?如果没有,最好的方法是什么?

pga*_*mou 18

好吧,我不知道为什么我会投票.但无论如何,我发现我的问题在看这个例子.

实际问题是我没有使用express来"公开"目录:

app.use(express.static(__dirname));
Run Code Online (Sandbox Code Playgroud)

这是你好世界

的index.html

<!DOCTYPE html>
<html ng-app="main">
<head>
    <meta name="name" content="something">
    <title></title>
</head>
<body>
    <section ng-view=""></section>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.min.js" type="text/javascript" charset="utf-8"></script>

    <script type="text/javascript" charset="utf-8">
    angular.module('main', ['ngRoute'])
    .config(['$routeProvider', function ($routeProvider){
        console.log('hola');
        $routeProvider
        .when('/', {
            templateUrl: 'templates/index.html'
            ,controller: 'indexCtrl'
        })
        .when('/second', {
            templateUrl: 'templates/second.html'
            ,controller: 'secondCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
    }])
    .controller('indexCtrl', ['$scope', function ($scope){
        $scope.helloWorld = "Hello World";
    }])
    .controller('secondCtrl', ['$scope', function ($scope){
        $scope.helloWorld = "World Hello";
    }]);
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Server/index.js

var express = require('express'),
app = express();

app.use(express.static(__dirname));
app.get('/', function(req, res) {
    res.sendfile('index.html', {root: __dirname })
});
var server = app.listen(process.env.PORT || 80);
Run Code Online (Sandbox Code Playgroud)

Procfile

web: node index.js
Run Code Online (Sandbox Code Playgroud)

模板/ index.html的

<h1>{{ helloWorld }}<h1>
<a href="#/second" title="">Go to Second</a>
Run Code Online (Sandbox Code Playgroud)

模板/ second.html

<h1>{{ helloWorld }}<h1>
<a href="#/" title="">Go to First</a>
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助别人.

回答我的问题.是的,有可能,我做错了是不能使模板(文件)可访问.如果您希望在可访问的内容上获得额外的安全性,则可以创建一个名为public的文件夹.然后将该目录设为静态:

app.use(express.static(__dirname + '/public'));
Run Code Online (Sandbox Code Playgroud)

那么你也可以有不同的路由与REST应用程序进行通信.喜欢:

app.get('/users', function(req, res) {
    res.json({...});
});
Run Code Online (Sandbox Code Playgroud)