2016-08-19 68 views
0

我正在嘗試更多地使用MEAN,並且我遵循Thinkster's Tutorial。我正在轉移到Node.JS的部分,但到目前爲止我已經遇到了很多問題,我相信它不在我的代碼中。本地主機vs Codepen上的節點腳本呈現不同

我終於決定通過'npm start'加載它,並且在本地主機上它仍然看起來不正確。我大部分時間都沒有看到評論,有時我會評論,但{{評論}}等內容無法解決。我原本以爲是因爲在本地打開文件,所以認爲NPM可以修復它。

我正在使用'Brackets',它很好,直到它變成EJS,然後再也無法預覽它了。

什麼困惑我的是我張貼到Codepen,且該網站正常工作相當(除了點擊標題不回去指數,但我不相信我們做過該控制器。

我Index.ejs

<html> 
<head> 
    <title>Flapper News</title> 
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"> 

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script> 
    <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script> 
    <script src="javascripts/angularApp.js"></script> 
    <style> .glyphicon-thumbs-up { cursor:pointer } </style> 
</head> 

<script type="text/ng-template" id="/home.html"> 
    <div class="page-header"> 
     <h1>Flapper News</h1> 
    </div> 
</script> 

<script type="text/ng-template" id="/posts.html"> 
    <div class="page-header"> 
    <h3> 
     <a ng-show="post.link" href="{{post.link}}"> 
     {{post.title}} 
     </a> 
     <span ng-hide="post.link"> 
     {{post.title}} 
     </span> 
    </h3> 
    </div> 

    <div ng-repeat="comment in post.comments | orderBy:'-upvotes'"> 
    <span class="glyphicon glyphicon-thumbs-up" 
     ng-click="incrementUpvotes(comment)"></span> 
    {{comment.upvotes}} - by {{comment.author}} 
    <span style="font-size:20px; margin-left:10px;"> 
     {{comment.body}} 
    </span> 
    </div> 

<form ng-submit="addComment()" 
    style="margin-top:30px;"> 
    <h3>Add a new comment</h3> 

    <div class="form-group"> 
    <input type="text" 
    class="form-control" 
    placeholder="Comment" 
    ng-model="body"></input> 
    </div> 
    <button type="submit" class="btn btn-primary">Post</button> 
</form> 

</script> 

<body ng-app="flapperNews" ng-controller="MainCtrl"> 
    <div class="row"> 
     <div class="col-md-6 col-md-offset-3"> 
      <ui-view></ui-view> 

      <div ng-repeat="post in posts | orderBy:'-upvotes'"> 
       <span class="glyphicon glyphicon-thumbs-up" 
        ng-click="upvote(post)"></span> 
       {{post.upvotes}} 
       <span style="font-size:20px; margin-left:10px;"> 
        <a ng-show="post.link" href="{{post.link}}"> 
         {{post.title}} 
        </a> 
        <span ng-hide="post.link"> 
         {{post.title}} 
        </span> 
         <span> 
          <a href="#/posts/{{$index}}">Comments</a> 
         </span> 
       </span> 
      </div> 

      <form ng-submit="addPost()" 
       style="margin-top:30px;"> 
       <h3>Add a new post</h3> 

       <div class="form-group"> 
        <input type="text" 
         class="form-control" 
         placeholder="Title" 
         ng-model="title"></input> 
       </div> 
       <div class="form-group"> 
        <input type="text" 
         class="form-control" 
         placeholder="Link" 
         ng-model="link"></input> 
       </div> 
       <button type="submit" class="btn btn-primary">Post</button> 
      </form> 

     </div> 
    </div> 
</body> 
</html> 

我angularApp.JS

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

app.config([ 
'$stateProvider', 
'$urlRouterProvider', 
function($stateProvider, $urlRouterProvider) { 

    $stateProvider 
     .state('home', { 
      url: '/home', 
      templateUrl: '/home.html', 
      controller: 'MainCtrl' 
     }).state('posts', { 
      url: '/posts/{id}', 
      templateUrl: '/posts.html', 
      controller: 'PostsCtrl' 
     }); 

    $urlRouterProvider.otherwise('home'); 
}]) 

app.factory('posts', [function(){ 
    var o = { 
     posts: [] 
    }; 
    return o; 
}]); 

app.controller('MainCtrl', [ 
'$scope', 
'posts', 
function($scope, posts){ 
    $scope.posts = posts.posts; 
    $scope.addPost = function(){ 
     if(!$scope.title || $scope.title === '') { return; } 
     $scope.posts.push({ 
      title: $scope.title, 
      link: $scope.link, 
      upvotes: 0, 
      comments: [ 
       {author: 'Joe', body: 'Cool post!', upvotes: 0}, 
       {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0} 
      ] 
     }); 
     $scope.title = ''; 
     $scope.link = ''; 
    } 
    $scope.upvote = function(post){ 
     post.upvotes++; 
    } 
}]); 

app.controller('PostsCtrl', [ 
'$scope', 
'$stateParams', 
'posts', 
function($scope, $stateParams, posts){ 
    $scope.post = posts.posts[$stateParams.id]; 
    $scope.addComment = function(){ 
     if($scope.body === '') { return; } 
     $scope.post.comments.push({ 
      body: $scope.body, 
      author: 'user', 
      upvotes: 0 
     }); 
     $scope.body = ''; 
    }; 
}]); 

本來我確實認爲這是由於它是本地的,但不會NPM解決這個問題嗎?如果沒有,那麼在您使用Node/Express時真正測試Node/Express的最佳方法是什麼?

感謝任何見解。

(我環顧四周SO,我看到很多類似問題的礦井的帖子,但沒有回答直接/更新)

的package.json

{ 
    "name": "flapper-news", 
    "version": "0.0.0", 
    "private": true, 
    "scripts": { 
    "start": "node ./bin/www" 
    }, 
    "dependencies": { 
    "body-parser": "~1.15.1", 
    "cookie-parser": "~1.4.3", 
    "debug": "~2.2.0", 
    "ejs": "~2.4.1", 
    "express": "~4.13.4", 
    "mongoose": "^4.5.9", 
    "morgan": "~1.7.0", 
    "serve-favicon": "~2.3.0" 
    } 
} 

中添加Console.Log

我的控制檯正在返回一個404 GET響應。不確定原因。

angular.js:9818GET http://localhost:3000/home.html 404 (Not Found)(anonymous function) @ angular.js:9818m @ angular.js:9619Ke.$get.f @ angular.js:9335(anonymous function) @ angular.js:13175$eval @ angular.js:14388$digest @ angular.js:14204$apply @ angular.js:14493(anonymous function) @ angular.js:1449e @ angular.js:4182d @ angular.js:1447sc @ angular.js:1467Jd @ angular.js:1361(anonymous function) @ angular.js:26086a @ angular.js:2741c @ angular.js:3011 
angular.js:9818 GET http://localhost:3000/home.html 404 (Not Found)(anonymous function) @ angular.js:9818m @ angular.js:9619Ke.$get.f @ angular.js:9335(anonymous function) @ angular.js:13175$eval @ angular.js:14388$digest @ angular.js:14204$apply @ angular.js:14493l @ angular.js:9650O @ angular.js:9840w.onload @ angular.js:9781 
+0

既然你引用'npm start',這裏可以包含你的package.json,這樣我們就可以看到實際運行的是什麼,對於我來說這個問題還不是很清楚。我知道有些東西不起作用,但不清楚是什麼?你有沒有看過網絡檢查員,看看哪些負載和控制檯中沒有任何錯誤,發生了什麼你期望的是什麼? (僅僅是你在視圖中看到{{}}這樣的插值大括號?通常這意味着你在控制檯中有角度錯誤,因爲某些東西沒有解析或頁面沒有處理導致角度沒有加載) – shaunhusain

+0

嘿@shaunhusain - 根本不是渲染。在某種意義上,不是顯示主題名稱'foo',而是顯示{{post.title},並且該鏈接也不反映控制器。 – DNorthrup

+0

控制檯中的任何錯誤都應該在那裏?如果控制檯中沒有錯誤,接下來我要檢查的是angular.js的網絡選項卡篩選器,並檢查響應實際上是否加載了angular.js源代碼...角度等待DOMContentReady,然後開始解析尋找ng-app指令的頁面,首先發現它觸發引導過程(不配置然後運行塊,然後開始尋找更多指令來處理)。 – shaunhusain

回答

0

它說它找不到home.html。您的網址在home狀態可能是錯誤的。這個網址是相對於你的index.html文件,所以仔細檢查。

+0

我甚至沒有想過這個。我假設自從使用模板以後,這並不重要。所以我將它們更新爲'views/{{templateUrl}}',404消失了,但它仍然沒有加載任何東西。 – DNorthrup

+0

控制檯中的任何其他錯誤? –

+0

根本不是。與其他人討論,他們認爲這可能是一個緩存問題,並且該頁面在Angular之前加載。不確定。 – DNorthrup