【问题标题】:Angular / Express hostingAngular / Express 托管
【发布时间】:2015-09-08 12:08:21
【问题描述】:

我想在不需要 node 或 express 的情况下在 linux 机器上运行 angular。我已经创建了一个网站,但不确定是什么技术,哈哈。我假设我有一个使用快速服务器的简单 Web 服务器,请参见下面的代码。

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

app.use(express.static(__dirname + '/'));


app.listen(8080);
console.log('Magic happens on port 8080');

我使用节点服务器命令启动它。其余代码是angular-ui。

我是否需要使用 express(并将其托管在节点兼容的服务器上),或者我可以在没有 express 的 linux 机器上运行这个东西吗?如果是这样,我是否需要用其他东西替换我的 server.js 文件(上面)?或者...目前它不能在 linux 机器上工作,但在本地工作得很好。

**编辑:我在共享服务器上测试了一个有角度的“hello world”应用程序,它运行良好。当我在共享服务器上运行完整的 Angular 应用程序时,出现以下错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module routerApp due to:
Error: [$injector:nomod] Module 'routerApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

** 编辑:在回答@RobertMoskal 下面的问题时,在共享服务器上运行的角度hello world 测试基本上是这样的:

<input ng-model="name" type="text" placeholder="Type a name here">
<h1>Hello {{ name }}</h1>

而真正的app基本上是这样的,在html中使用ui-view和ng-repeat:

var routerApp = angular.module('routerApp', ['ui.router']);

routerApp.config(function($stateProvider, $urlRouterProvider, $locationProvider) {

    $urlRouterProvider.otherwise('/home');
    $locationProvider.html5Mode(false).hashPrefix("");
    $stateProvider


        // HOME STATES AND NESTED VIEWS ========================================
        .state('home', {
            url: '/home',
            templateUrl: 'partial-home.html',
         //   onEnter: scrollContent
        })

        // ANIMATION AND NESTED VIEWS ========================================
        .state('animation', {
            url: '/animation',
            templateUrl: 'partial-anim.html', 

        controller: function($scope) {
                $scope.animations = [
                { title:'One', url:'http://yahoo.com', bg:'#f8f8f8', width:'160', height:'600', imageAsset:'assets/imgs/web/MyWebsites_1.jpg', paragraph:'some text of some description'},
                { title:'Two', url:'http://google.com', bg:'#f8f8f8', width:'160', height:'600', imageAsset:'assets/imgs/web/MyWebsites_2.jpg', paragraph:'rabbit rabbit rabbit'},
                { title:'Three', url:'http://bambam.com', bg:'#f8f8f8', width:'160', height:'600', imageAsset:'assets/imgs/web/MyWebsites_3.jpg', paragraph:'blahiblahblah'}];
            }
        })


          // GAME VIEWS ========================================
        .state('game', {
            url: '/game',
            templateUrl: 'partial-game.html'
        })


        // CONTACT VIEWS ========================================
        .state('contact', {
            url: '/contact',
            templateUrl: 'partial-contact.html'
        })


});

【问题讨论】:

    标签: linux angularjs node.js angular-ui-router


    【解决方案1】:

    您需要一些 Web 服务器来将您的 Angular 应用程序作为“静态”资产提供服务。这可以是 apache 或 nginx 或任意数量的 Web 服务器。大多数 linux 发行版都可以轻松安装它们。

    您还可以使用内置的 Python Web 服务器实现超轻量级:

    cd /var/www/
    $ python -m SimpleHTTPServer
    

    您甚至可以在 github 上免费托管您的应用程序。

    在所有情况下,您只需要确保网络服务器从正确的路径为您的资产提供服务。上面的 python 示例示例中,您的应用程序入口点可能位于 /var/www/index.html 中,它会被用作 http://localhost:8000/index.html

    【讨论】:

    • 酷,我如何将它作为静态资产托管?现在它不能在共享主机环境中的 linux 机器上工作:(
    • 好的,所以我必须在服务器上初始化这个 python 东西?我在一个共享的盒子上,我认为我的主机不允许这样做:(它是 apache,我怎样才能让它正常工作?
    • hmmm... 好吧,如果应用程序需要运行在不同的路径上怎么办?例如,/folder1/folder2/?
    • 您需要为您的 apache 站点设置一个文档根目录。如果这是一个共享主机设置,您可能有某种控制面板。否则,您将需要手动执行此操作。文档根目录要么指向 Angular 应用所在的目录,要么你将 Angular 应用复制到该目录中。
    • 我的主机告诉我 Angular 是一个必须在帐户上运行的守护进程,我必须为此使用 VPS :( 有什么可以支持 Angular 的廉价主机的想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2016-11-21
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多