2

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 Answer 1

5

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');
});
Sign up to request clarification or add additional context in comments.

2 Comments

to explain: the problem is that your JS file isn't being served - you are only telling express to serve '/'
I just woke up and tried it, it worked and and it's awesome :) But why is that working now ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.