0

I used Chrome to open the index.html page and the error is cannot find right modules.

Uncaught SyntaxError: Unexpected token < angular.js:1
Uncaught SyntaxError: Unexpected token < controller.js:1
Uncaught SyntaxError: Unexpected token < service.js:1
Uncaught SyntaxError: Unexpected token < main.js:1

I used Firefox and got the following similar errors

SyntaxError: expected expression, got '<'


<html ng-app="todo">

angular.js (line 1)
SyntaxError: expected expression, got '<'


<html ng-app="todo">

controller.js (line 1)
SyntaxError: expected expression, got '<'


<html ng-app="todo">

service.js (line 1)
SyntaxError: expected expression, got '<'


<html ng-app="todo">

main.js (line 1)

I have been finding out where I made mistakes for a while, but still no clues. I copied and pasted the code here and thanks in advance for pointing me out. Code is as bellow shows:

service.js file

var service = angular.module("service",[]);

service.factory("todoFactory", ["$http",
    function($http){
        return {
            get: function(){
                return $http.get('/todo');
            },
            create: function(newtodo){
                return $http.post('/todo', newtodo);
            },
            delete: function(id){
                return $http.delete('/todo:' + id);
            }
        }
}]);

controller.js file

var ctrl = angular.module("controller", []);
ctrl.controller("mainController", ['$http', '$scope', 'todoFactory', function($scope, $http, todoFactory){
    $scope.formData = {};


    todoFactory.get().success(function(data){
        $scope.todoitems = data;
    });


    if(!$scope.formData.title && !$scope.formData.content){
        //var newtodoData = {
        //    "title":$scope.formData.title,
        //    "content" : $scope.formData.content
        //}
        todoFactory.create($scope.formData)
                   .success(function(data){
                        //return the updated todoitems
                        $scope.todoitems = data;
                        //clear form data for next time use
                        $scope.formData = {};
                    });
    }

}]);

main.js file

angular.module("todo", ["controller", "service"]);

index.html

<html ng-app="todo">
    <head>
        <script src="frontend/angular.js"></script>
        <script src="frontend/controller.js"></script>
        <script src="frontend/service.js"></script>
        <script src="frontend/main.js"></script>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css">
    </head>

    <body ng-controller="mainController">
        <form class="form-inline" ng-submit="submitForm()">
            <div class="form-group">
                Title:
                <input class="form-control col-md-4" type="text" ng-model="formData.title"><br>
            </div>
            <div class="form-group">
                Content:
                <input class="form-control col-md-6" type="text" ng-model="formData.content"><br>
            </div>
            <button class="btn btn-primary" type="submit">Add</button>
        </form>

        <div>
            <p>Here is your todo list:</p>
                <div ng-repeat="item in todoitems"></div>
                <div class="col-md-3">{{item.title}}</div>
                <div class="col-md-6">{{item.content}}</div>
        </div>

    </body>

</html>

Following is my server side code

server.js

//set up all the modules needed
var express = require("express");
var bodyParser = require("body-parser");
var morgan = require("morgan");

var app = express();
var todoModel = require('./model.js');

//use part for middle layers
app.use(morgan('dev'));                                          
app.use(bodyParser.urlencoded({'extended':'true'}));             
app.use(bodyParser.json());                                      
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));

var port = process.env.PORT || 9000;
require('./route.js')(app);

app.listen(port, function() {
    console.log("Listening on " + port);
});

Route.js in backend

var todoModel = require('./model.js');


function getAllTodos(res){
    todoModel.find(function(err, alltododata){
        if(err)
            res.send(err);
        res.json(alltododata);
    });
}

module.exports = function(app){

    //render the view page
    app.get('*', function(req, res) {
        res.sendfile('B:\\node\\ToDo\\NodeTODOMongo\\index.html'); // load the single view file (angular will handle the page changes on the front-end)
    });


    //get all todos return json data
    app.get('/todo', function(req, res){
        getAllTodos(res);
    });


    //create items and send back all todos after creation
    app.post('/todo', function(req, res){
        todoModel.create({
            title : req.body.title,
            content: req.body.content
        }, function(err, todo){
            if(err){
                res.send(err);
            }
            getAllTodos(res);
        });
    });


    app.delete('/todo:id', function(req, res){
        todoModel.remove({
                _id : req.params.id
            }, //what is the alltodo here?
            function(err, todo){
                if(err)
                    res.send(err);
                getAllTodos(res);
            });
    });
}

model.js

var mongoose = require('mongoose');

//connect to localhost default test db
mongoose.connect('mongodb://localhost/test');

var db = mongoose.connection;

//let me know if the connection errors out
db.on('error', console.error.bind(console, 'connection error:'));


db.once('open', function callback () {
  console.log("h");
});

//define schema of the todo model
var schema = new mongoose.Schema({
    title: String,
    content: String,
    createDate: {type: Date, default:Date.now}
});

var model = mongoose.model("todo", schema);

module.exports = mongoose.model("todo", schema);
11
  • 1
    can you upload main.js file somewhere? Commented Mar 29, 2015 at 20:53
  • @NikhilBhandari Thanks for reply. main.js file only contains one line of code. I've already put that in the code snippet. Commented Mar 29, 2015 at 21:36
  • I just wanted to see the file, sometimes there are invisible characters that get copied when you copy something from a web page. I tried running your code and it worked on my machine so its not your code its either some invisible character or infected browser/machine . :) Commented Mar 29, 2015 at 21:45
  • Try the opening it in a different browser. Commented Mar 29, 2015 at 21:45
  • I tried using both Firefox and Chrome and got the same results...... Commented Mar 29, 2015 at 21:48

1 Answer 1

2

The line app.get('*', function(req, res) { in your node server is the problem here. Basically, you have told the server that no matter what request comes in, it should return res.sendfile('B:\\node\\ToDo\\NodeTODOMongo\\index.html');. This results in requests for your .js files causing the server to return index.html instead of the actual file requested.

You can add app.use('/frontend', express.static(__dirname + '/frontend')); before your app.get to enable a static request pipeline for the /frontend directory, which appears to be where your .js files are stored.

As pointed out by @GregL, you will also have issues with the other routes you have defined after the app.get('*',....

I would recommend Bran Ford's blog post on correctly configuring angular + express.

Sign up to request clarification or add additional context in comments.

2 Comments

In other words, Express evaluates what route to run based on the order you specify routes in your code. By specifying a route which matches every GET request first, it doesn't matter what other .get() routes you define later, this first one will handle it.
Thank you for your comments. I understand it now and it does help me. @GregL

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.