Robin Robin - 4 months ago 27
AngularJS Question

not able to load java script files and bower_components/angular/angular.js

When i run the localhost:3000 server , i'm getting these errors in the console:

http://localhost:3000/bower_components/angular/angular.js Failed to load resource: the server responded with a status of 404 (Not Found)

http://localhost:3000/controller.js Failed to load resource: the server responded with a status of 404 (Not Found)

It was working if i put the angular.js script in the index.html instead of putting it seperately in controller.js , i don't know why i'm getting an error when i use script tags.

the directory diagram is like this

-files
-bower_components
-angular
-index.html
-controller.js
-server.js


this is the index.html file

<!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>&nbsp;
<button ng-click="updatetask()">Update</button>&nbsp;<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>&nbsp;&nbsp;<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>


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

Answer

In your server.js file add following lines and check it once.

app.use(express.static(__dirname));

in node js you have to set registry for accessing a relative paths.