AngularJS代码不能与NodeJS一起使用

时间:2014-12-14 22:20:54

标签: angularjs node.js mean-stack

我是MEAN Stack的新手。我在html中尝试了一些非常基本的东西,当我在浏览器中打开它时,会使用一些angularJS。但遗憾的是,当我尝试使用nodeJS服务器渲染代码时,代码不再起作用了。显示index.html但角度部分不再起作用。我的输出只是{{article}}。你有什么建议吗?

的index.html

<html ng-app="ibrahimsBlog">

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Ibrahims Blog</title>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    <script src="app.js"></script>
  </head>

  <body>

  <div ng-controller="articleController">
    <div ng-repeat="article in articles">
      {{article}}
    </div>
  </div>  

  </body>

</html>

app.js

var app = angular.module('ibrahimsBlog', [])
app.controller('articleController', [
'$scope',
function($scope) {
  $scope.articles = [
    'foo',
    'bar',
    'baz'
  ];
}]);

我非常基本的节点服务器:

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

app.get('/', function(req, res) {
  res.sendFile(__dirname + '/index.html');
});

app.listen(3000, function() {
  console.log('Server listening on port 3000');
});

1 个答案:

答案 0 :(得分:5)

SERVER.JS 更改为:

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

/* ==========================================================
serve the static index.html from the public folder
============================================================ */
app.use(express.static(__dirname + '/public'));

app.listen(3000, function() {
  console.log('Server listening on port 3000');
});