2015-01-11 27 views
0

我使用node.js與express和Angularjs來建立我的網站的網頁, 但我在控制檯中得到以下輸出 看起來程序無法找到我的腳本文件位於程序找不到我的腳本文件

GET http://localhost:3000/images/entet_gauche.gif 404 (Not Found) 
GET http://localhost:3000/js/app.js 
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.8/$injector/modulerr?p0=MetaStore&p1=Error%…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.8%2Fangular.min.js%3A17%3A350) 

這似乎是程序無法找到位於 我的腳本文件,什麼是錯的路徑?

的index.html

<body ng-app="MetaStore"> 
    <a class="navbar-brand" href="#"> 
    <img alt="Brand" src="./images/entet_gauche.gif"> 
    </a> 

    <div ng-controller="produContr" class="container"> 
    <div class="container-fluid"> 
     <div class="row"> 

     <div ng-repeat="product in products | filter:search:strict" class="col-sm-3"> 
      <h3> {{product.name}} </h3> 
      <p> {{product.content}} </p> 
      <p><strong>Prix : </strong> {{product.prix}} DH</p> 
     </div> 

     </div> 

    </div> 
    </div> 

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 

    <script src="/js/app.js"></script> 

</body> 

JS/app.js

變種應用= angular.module( 'MetaStore',[]); app.controller( 'produContr',函數($範圍,$ HTTP,$間隔){

})

server.js

var express = require("express"); 
var mysql = require("mysql"); 
var app = express(); 

var connection = mysql.createConnection({ 
    host: "localhost", 
    user: "root", 
    password: "", 
    database: "angularjsDB" 
}); 

connection.connect(function(error) { 
    if (error) { 
    console.log("Problem with MySQL" + error); 
    } else { 
    console.log("Connected with Database"); 
    } 
}); 

app.get('/', function(req, res) { 
    res.sendFile(__dirname + '/index.html'); 
}); 

app.get('/load', function(req, res) { 
    connection.query("SELECT * from product", function(err, rows) { 
    if (err) { 
     console.log("Problem with MySQL" + err); 
    } else { 
     res.end(JSON.stringify(rows)); 
    } 
    }); 
}); 

app.listen(3000, function() { 
    console.log("It's Started on PORT 3000"); 
}); 

更新: 這是我的目錄結構:

index location -> (folder/index.html) ... 
server location -> (folder/server.js) ... 
app location -> (folder/js/app.js) ... 
picture location ->(folder/images/entet_gauche.gif) 

感謝您的關注和考慮看看

+0

嘗試'src =「images/entet_gauche.gif」'和'src =「// images/entet_gauche.gif」'。只是出於好奇! – surajck

+0

不工作我的朋友 – user3514348

+0

你可以發佈你的目錄結構看起來像什麼嗎? – lhoworko

回答

2

看起來你沒有設置靜態文件中間件。在你做的路線,添加:

app.use(express.static(__dirname + '/public'));

其中「公開」是你的CSS和JS文件的文件夾。查看快遞文檔,瞭解您可能需要的其他選項。

+0

在這種情況下猜測它會是app.use(express.static('js'))在他的例子中? –

+0

不一定,大多數快遞應用程序的默認設置是在其中包含一個帶有js文件夾的公用文件夾。在該設置中,使用我的示例代碼,js文件夾的URL路徑將爲「/ js」,因爲除非另行配置,否則靜態中間件會將指定的文件夾安裝在根URL上。 – Paul

+1

app.use(「/ images」,express.static(__ dirname +'/ images')); (「/ js」,express.static(__ dirname +'/ js')); – user3514348