2016-05-31 58 views
0

这是server.js文件

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");

这是todo.html文件

<!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>

这是frontend.js文件

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=""; 
 
} 
 

 
}]);

这是错误,它示出了在浏览器控制台

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
我试图通过连接到本地主机//显示todo.html文件: 3000,该文件已显示,但包含todo.html的javascript的frontend.js文件未加载,并且在浏览器控制台中也抛出了一些错误。任何关于如何在连接到本地主机服务器时将frontend.js精确导入到todo.html文件的帮助将有所帮助

+0

我在加载本地文件时遇到了问题,因为某些浏览器会阻止它们。您使用的是哪种浏览器?它是否具有防止加载本地文件(即直接来自硬盘的文件)的设置。 您应该尝试设置一个本地HTTP服务器并通过它加载文件以查看它是否开始工作。 – Paurian

+0

我不知道如何通过本地http服务器加载文件,我可以通过它简要说明 – Robin

+0

安装一个本地web服务器往往会因操作系统而异 - 但您可以尝试Apache http://httpd.apache.org /让你通过设置它对于这个特定的线程来说有点多。 你在浏览什么?也许我们可以从接近权限中找到更直接的解决方法? – Paurian

回答

0

合并两个JS文件后,我运行了代码。它按预期运行,遇到了一个调用todolist Web服务的问题(因为我没有在我的机器上运行该服务)。

请注意,我没有调用任何本地文件的src。本地文件对浏览器是一种安全风险,没有设置权限让它们忽略这种风险,它们不会从本地驱动器(使用HTTP服务器之外)加载JavaScript。

请让我知道您的浏览器的类型,我会看看我是否可以帮助您解决问题的部分 - 您可能会遇到更多问题,因为您没有从网站加载这些文件服务器,您的Web服务调用需要。

如果您使用http://localhost:3000获得成功的结果,那么您的计算机上运行着一个活动的Web服务器,或者您正在另一台计算机上通过浏览器运行该URL(例如通过TS会话)。如果你确定你的“localhost”是你的机器,你应该尝试从你的web服务器从中提取文件的目录下工作。

<!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> 
+0

我也是通过合并这两个文件来运行代码,但按钮不能正常工作,它们只是死了,请你看看所有的3个文件,并看看为什么待办事项列表应用程序不起作用 – Robin

+0

我不希望按钮的工作原因或几个原因:1.您的浏览器将不被允许从本地文件执行跨源请求(file:/// URI)。 2.您的服务器可能未提供todolist的请求。如果您在浏览器中并且输入http:// loclhost:3000/todolist/X,则您的服务器代码看起来不会处理任何错误(如果“X”无效)。将日志记录代码全部[仅在调试期间]查看是否有助于您理解 - 检查控制台消息是否有404或500错误。 (我已更新代码以反映此建议)。 – Paurian

相关问题