AngularJS Code not working with NodeJS

懵懂的女人 提交于 2019-12-24 02:43:13

问题


I am new to the MEAN Stack. I tried out something pretty basic in html with some angularJS which is working when I open it in my browser. But unfortunately the code is not working anymore when I try to render it with my nodeJS server. The index.html is shown but the angular part is not working anymore. My output is just {{article}}. Do you have any suggestions ?

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'
  ];
}]);

And my pretty basic node server:

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:


Change SERVER.JS to:

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');
});


来源:https://stackoverflow.com/questions/27474801/angularjs-code-not-working-with-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!