0

this is the server.js file

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
app.use(bodyParser.json());

mongoose.connect("mongodb://localhost/test");

var todoschema = new mongoose.schema ({
 name : {type: String, required: true}
 });

var todomodel = mongoose.model('todolist',todoschema);

app.get('/',function(req,res){
   res.sendFile('C:\\Users\\Rohit\\Desktop\\New folder\\todo.htm');
});

app.get('/todolist', function (req, res){
    todomodel.find(function(err,tasks){
      res.json(tasks);
     });
});

app.post('/todolist', function (req, res) {
  
  todomodel.insert(req.body, function(err, task) {
    res.json(task);
  });
});

app.delete('/todolist/:id', function (req, res) {
  
 todomodel.remove(req.params.id, function (err, task) {
    res.json(task);
  });
});

app.get('/todolist/:id', function (req, res) {
  
  todomodel.findById({req.params.id, function (err, task) {
    res.json(task);
  });
});

app.put('/todolist/:id', function (req, res) {
  
  todomodel.findAndModify({
     query: req.params.id,
     update: {$set: {name: req.body.name}},
     new: true}, function (err, task) {
      
      res.json(task);
    }
  );
});

app.listen(3000);
console.log("Server running on port 3000");

This is the todo.html file

<!DOCTYPE html>
<html ng-app="App">
<head>


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
<script src="C:\Users\Rohit\Desktop\New folder\frontend.js">


</script>

<style>
#list   
{ margin-left:320px;
  font-size: 40px;
  font-family:verdana;
}
button     
{ color:yellow;background-color:red;text-align:center;cursor:pointer;
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
  font-size:40px;
  padding: 14px 32px;
}
button:hover
{ background-color:Peachpuff;
  color:tomato;
}
</style>
</head>

<body style="background-color:cyan;">

<div ng-controller="Ctrl">

<h1 style="text-align:center;font-family:verdana;">To-Do LiSt</h1>

<div style="margin-left:300px">
<input type="text" ng-model="task.name" style="background-color:black;color:white;font-size:40px;width:40%">
<button ng-click="addtask()">Add</button>&nbsp;<button ng-click="updatetask()">Update</button><button ng-click="clearfield()">Clear</button>&nbsp;
</div>

<ul>
<li id="list" ng-repeat="task in todolist">
{{task.name}}

<button ng-click="deletetask(task._id)">Delete</button>&nbsp;<button ng-click="edittask(task._id)">Edit</button>
</tr>
</table>

</div>


</body>
</html>

This is the frontend.js file

var App = angular.module('App',[]);
App.controller('Ctrl',['$scope','$http',function($scope,$http) {
         
    var reset = function(){
      $http.get('/todolist').success(function(response){
        $scope.todolist=response;
        $scope.task="";
        });
     };

  reset();

$scope.addtask = function() {
  $http.post('/todolist', $scope.task).success(function(response) {
       reset();
    });
 };

$scope.deletetask = function() {
  $http.delete('/todolist/'+ id).success(function(response){
      reset();
      });
   };
 
$scope.edittask = function(id) {
   $http.get('/todolist/'+ id).success(function(response){
       $scope.task=response;
     });
   };

$scope.updatetask = function(id){
   $http.put('/todolist/'+$scope.task._id, $scope.task).success(function(response){
       reset();
    });
 };

$scope.clearfield = function(){
   $scope.task="";
}

}]);

This is the error , it shows in the browser console

Not allowed to load local resource: file:///C:/Users/Rohit/Desktop/New%20folder/server.js
angular.min.js:35Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.12/$injector/modulerr?p0=App&p1=Error%3A%20…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.12%2Fangular.min.js%3A17%3A381)
http://localhost:3000/favicon.ico Failed to load resource: the server responded with a status of 404 (Not Found)
Failed to parse SourceMap: https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js.map
I tried to display the todo.html file by connecting to //localhost:3000, the file was displayed , but the frontend.js file which contains the javascript of todo.html didn't load and it also threw a few errors in the brower console . Any help on how to exactly import the frontend.js into todo.html file while connecting to localhost server will be helpful
RohitB97
  • 440
  • 2
  • 6
  • 16
  • I've run into issues when loading local files because certain browsers block them. What browser are you using and does it have settings that prevent loading local files (i.e. files directly from your hard-drive). You should try setting up a local HTTP server and loading the files through that to see if it starts working. – Paurian May 31 '16 at 12:55
  • i don't know how to load files through local http server , can u brief me through it – RohitB97 May 31 '16 at 13:02
  • Installing a local web server tends to vary from one OS to another - but you can try Apache http://httpd.apache.org/ walking you through getting it set up is a bit much for this specific thread. What browser are you on? Perhaps we can find a more direct work-around from approaching the permissions? – Paurian May 31 '16 at 13:26
  • I have seen examples where they were able to load the local files into their webpage in the same browser, why am i not able to do that – RohitB97 May 31 '16 at 13:28
  • What type of browser? IE? IE8? IE11? Safari? FireFox? Chrome? The permission settings are different in each browser. – Paurian May 31 '16 at 13:36
  • Also just noticed something - you're using the direct file path instead of prepending it with the file URI, meaning it might not even be a permissions issue. Look at this: http://stackoverflow.com/questions/3582671/how-to-open-a-local-disk-file-with-javascript – Paurian May 31 '16 at 13:38
  • In case you're on Chrome and having this issue, you might need to also modify the permissions settings : https://chrisbitting.com/2014/03/04/allow-local-file-access-in-chrome-windows/ – Paurian May 31 '16 at 13:47
  • i have tried putting the js code into the html itself , but i'm still getting the "failed to parse sourcemap "error and injection-module err – RohitB97 May 31 '16 at 13:57
  • I should have read through your error message more thoroughly. Sorry. Nlote how it states "the server.js" file? It sounds like there's a dependency on it somewhere that you didn't mention in your post. Could you please look for where/how that file is being referenced? Thanks! – Paurian May 31 '16 at 14:06
  • i have updated it with server.js file, please have a look at it – RohitB97 May 31 '16 at 14:38

1 Answers1

0

I ran through your code after merging the two JS files. It ran as expected, and it encountered an issue calling the todolist web service (expected since I don't have that service running on my machine).

Note that I'm not calling a src of any local files. Local files are a security risk to browsers and without setting permissions for them to ignore this risk, they won't load your JavaScript from the local drive (outside of using an HTTP server).

Please let me know the type of browser you have and I'll see if I can help you resolve that part of your issue - you will likely have more issues to follow since you aren't loading these files from a web server that your web service calls require.

If you were getting back a successful result with http://localhost:3000 then either you have an active web server running on your machine or you're running that URL from a browser in context of a different machine (e.g. through a TS session). If you are certain that you're "localhost" is your machine, you should try working from the directory that your web server is pulling files from.

<!DOCTYPE html>
<html ng-app="App">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
    <script>
        var App = angular.module('App', []);
        App.controller('Ctrl', ['$scope', '$http', function ($scope, $http) {
            var reset = function () {
                $http.get('/todolist').success(function (response) {
                    $scope.todolist = response;
                    $scope.task = "";
                    console.log("reset success: " + response);
                }).error(function (response, status, headers, config) {
                    console.log("reset error: " + status + ": " + response);
                });
            };

            reset();

            $scope.addtask = function () {
                $http.post('/todolist', $scope.task).success(function (response) {
                    reset();
                    console.log("addtask success: " + response);
                }).error(function (response, status, headers, config) {
                    console.log("addtask error: " + status + ": " + response);
                });
            };

            $scope.deletetask = function (id) {
                $http.delete('/todolist/' + id).success(function (response) {
                    reset();
                    console.log("deletetask success: " + response);
                }).error(function (response, status, headers, config) {
                    console.log("deletetask error: " + status + ": " + response);
                });
            };

            $scope.edittask = function (id) {
                $http.get('/todolist/' + id).success(function (response) {
                    $scope.task = response;
                    console.log("edittask success: " + response);
                }).error(function (response, status, headers, config) {
                    console.log("edittask error: " + status + ": " + response);
                });
            };

            $scope.updatetask = function (id) {
                $http.put('/todolist/' + id, $scope.task).success(function (response) {
                    reset();
                    console.log("updatetask success: " + response);
                }).error(function (response, status, headers, config) {
                    console.log("updatetask error: " + status + ": " + response);
                });
            };

            $scope.clearfield = function () {
                $scope.task = "";
            }

        }]);
    </script>

    <style>
        #list {
            margin-left: 320px;
            font-size: 40px;
            font-family: verdana;
        }

        button {
            color: yellow;
            background-color: red;
            text-align: center;
            cursor: pointer;
            -webkit-transition-duration: 0.4s;
            transition-duration: 0.4s;
            font-size: 40px;
            padding: 14px 32px;
        }

        button:hover {
            background-color: Peachpuff;
            color: tomato;
        }
    </style>
</head>

<body style="background-color:cyan;">
    <div ng-controller="Ctrl">

        <h1 style="text-align:center;font-family:verdana;">To-Do LiSt</h1>

        <div style="margin-left:300px">
            <input type="text" ng-model="task.name" style="background-color:black;color:white;font-size:40px;width:40%">
            <button ng-click="addtask()">Add</button>&nbsp;<button ng-click="updatetask()">Update</button><button ng-click="clearfield()">Clear</button>&nbsp;
        </div>

        <ul>
            <li id="list" ng-repeat="task in todolist">{{task.name}}
        </ul>
        <button ng-click="deletetask(task._id)">Delete</button>&nbsp;<button ng-click="edittask(task._id)">Edit</button>
    </div>
</body>
</html>
Paurian
  • 1,372
  • 10
  • 18
  • i too ran the code by merging both files , but the buttons aren't working as they should , they are just dead , could you please look into all the 3 files, and see why the todo list app isn't working – RohitB97 May 31 '16 at 14:41
  • I wouldn't expect the buttons to work or a couple of reasons: 1. Your browser won't be allowed to perform cross-origin requests from a local file (file:/// URI). 2. Your server might not be serving requests for todolist. If you're in your browser and you type in http://loclhost:3000/todolist/X your server code doesn't appear to handle any errors (if "X" is invalid). Put logging code all throughout [only while debugging] to see if that helps you understand - check the console messages for 404 or 500 errors. (I've updated the code to reflect this suggestion). – Paurian May 31 '16 at 18:32