当我运行localhost:3000服务器时,我在控制台中收到这些错误:
http://localhost:3000/bower_components/angular/angular.js无法加载资源:服务器响应状态为404(未找到)
http://localhost:3000/controller.js无法加载资源:服务器响应状态为404(未找到)
如果我将angular.js脚本放在index.html中而不是将它单独放在controller.js中,我就知道为什么我在使用脚本标签时出错了
目录图是这样的
-files
-bower_components
-angular
-index.html
-controller.js
-server.js
这是index.html文件
<!DOCTYPE html>
<html ng-app="App">
<head>
<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>
<button ng-click="updatetask()">Update</button> <button ng-click="clearfield()">Clear</button>
</div>
<ul>
<li id="list" ng-repeat="task in todolist">
{{task.name}}
<button ng-click="deletetask(task._id)">Delete</button> <button ng-click="edittask(task._id)">Edit</button>
</li>
</ul>
</div>
</body>
<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="controller.js"></script>
</html>
这是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("/index.html",{root:__dirname});
});
app.get('/todolist', function (req, res){
TodoModel.find(function(err,tasks){
res.json(tasks);
});
});
app.post('/todolist', function (req, res) {
TodoModel.create({name:req.body.name},function(err,task){
res.send('created');
});
});
app.delete('/todolist/:id', function (req, res) {
TodoModel.remove({_id:req.params.id}, function (err) {
res.send('');
});
});
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.findByIdAndUpdate(req.params.id,
{name: req.body.name},
function (err, task) {
res.json(task);
}
);
});
app.listen(3000);
console.log("Server running on port 3000");
答案 0 :(得分:1)
在server.js文件中添加以下行并检查一次。
app.use(express.static(__dirname));
在节点js中,您必须设置注册表以访问相对路径。