0

I'm a begginer in angularJS, Node and Express and am at the very beginning of a tutorial to code a very simple To-do List using angularJS, Node and Express. I am using Node.js v7.9.0, Express.js v4.15.2, and AngularJS v1.6.1. I am using nodemon to run my server.js.

I keep getting 3 errors and I don't understand why:

SyntaxError: expected expression, got '<'[En savoir plus]  app.js:1
SyntaxError: expected expression, got '<'[En savoir plus]  route.js:1
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
[$injector:nomod] Module 'myApp' 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.
http://errors.angularjs.org/1.6.1/$injector/nomod?p0=myApp
minErr/<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:68:12
module/<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:2183:17
ensure@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:2107:38
module@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:2181:14
loadModules/<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:4736:22
forEach@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:357:11
loadModules@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:4720:5
createInjector@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:4642:19
doBootstrap@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:1838:20
bootstrap@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:1859:12
angularInit@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:1744:5
@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:32977:5
trigger@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:3314:5

This is my files path :

-todolist
--node_modules
--public
---views
----home.hmtl
---app.js
---index.html
---route.js
--package.json
--server.js

My server.js file :

var express = require('express');
var app = express();
var bodyparser= require('body-parser');
var method = require('method-override');

//application
app.get('*', function(req, res){
    res.sendFile('/public/index.html', {root : __dirname});
});

app.listen(3000);

My app.js :

angular.module('myApp', ['appRoutes']);

My route.js :

angular.module('appRoutes', ['ngRoute'])
.config(function($routeProvider){
   $routeProvider
       .when('/', {
                    templateUrl: '/views/home.html'
                  }
            )
});

My index.html :

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.js"></script>
        <script src="app.js"></script>
        <script src="route.js"></script>
        <title>Une To-Do Liste</title>
    </head>
    <body ng-app="myApp">
        <h1>To-do List</h1>
        <div ng-view></div>
    </body>
</html>

And the home.html contains a simple test, but fails to display :

<h1>Hello</h1>

For the moment the website only display the string 'To-do List' from my index.html. Why do I get 2 syntax errors from my app.js and route.js ? And why is my 'myApp' module not defined ?

3 Answers3

1

My app.js :

 angular.module('myApp', ['ngRoute']);

My route.js :

 angular.module('myApp')
    .config(function($routeProvider){
       $routeProvider
           .when('/', {
                        templateUrl: '/views/home.html'
                      }
                )
    });
    //with test controller
     angular.module('myApp')
        .controller('TestCtrl',['$scope', function($scope){
           $scope.it = 'works';
        }]);

index.html

 <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.js"></script>
            <script src="app.js"></script>
            <script src="route.js"></script>
            <title>Une To-Do Liste</title>
        </head>
        <body ng-app="myApp">
           <div ng-controller="TestCtrl"> 
              <h1>To-do List</h1>
             it {{it}}
          </div>
        </body>
    </html>
0

The problem was caused by missing inclusion of ngRoute module. Please take look at AngularJS 1.2 $injector:modulerr

Community
  • 1
  • 1
0

You need to change the sequence of file. As you are injecting appRoutes in the myApp it should be defined.

<script src="route.js"></script>
<script src="app.js"></script>

Define all required modules in app.js

angular.module('appRoutes', ['ngRoute']); //Define module
angular.module('myApp', ['appRoutes']);

In route.js, retrieve defined appRoutes and add config section

angular.module('appRoutes')
    .config(function($routeProvider){
       $routeProvider
           .when('/', {
                        templateUrl: '/views/home.html'
                      }
                )
    });
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thank you, but I keep geting the 2 syntax errors and the 3rd error is still telling me that the module 'myApp' is not available – Elya Bernard Apr 27 '17 at 12:38
  • @ElyaBernard, Go through http://stackoverflow.com/questions/28894074/syntaxerror-expected-expression-got – Satpal Apr 27 '17 at 12:43