2015-08-31 44 views
0

我對AngularJS頗爲陌生,並且使用$ http嘗試了一段新代碼。我已經安裝了nodeJS並使用npm安裝了express。當我在節點上運行server.js - node server.js時,我收到了郵件的收聽。但是當我嘗試在瀏覽器中加載index.html時,我收到一條錯誤消息,說它無法獲取文件。index.html即使在節點服務器運行成功時也不會加載

我的代碼: server.js

var express = require('express'), app = express(); 
// 
//app.configure(function(){ 
// app.use(express.static(__dirname,'/')); 
//}); 
app.listen(8080, 'localhost'); 

app.get('/customers/:id', function(req,res){ 
    var customerid = parseInt(req.params.id); 
    var data = {}; 

    for(var i=0, len = customers.length;i<len;i++){ 
     if(customers[i].id === customerid) { 
      data = customers[i]; 
      break; 
     } 
    } 
    res.json(data); 
}); 

app.get('/customers', function(req, res){ 
    res.json(customers); 
}); 



console.log('Express listening on port 8080'); 

var customers = [ 
      { 
       id:1, 
       joined: '2012-09-12', 
       name: 'John', 
       city:'Chandler', 
       orderTotal: 22.8699, 
       orders: [ 
        { 
         id: 1, 
         product:'shoes', 
         total: 9.9956 
        }, 
        { 
         id:2, 
         product: 't-shirts', 
         total: 12.8743 
        } 
       ] 
      }, 
      { 
       id:2, 
       joined: '2010-12-02', 
       name: 'Tina', 
       city:'New York City', 
       orderTotal: 10.345, 
       orders: [ 
        { 
         id:1, 
         product: 'shoes', 
         total: 10.345 
        } 
       ] 
      }, 
      { 
       id:3, 
       joined: '2013-10-25', 
       name:'Dave', 
       city:'Seattle', 
       orderTotal: 15.534, 
       orders: [ 
        { 
         id:1, 
         product: 'shoes', 
         total: 8.453 
        }, 
        { 
         id:2, 
         product: 'shorts', 
         total: 7.081 
        } 

       ] 
      }, 
      { 
       id:4, 
       joined: '1998-01-15', 
       name:'Zed', 
       city:'Las Vegas', 
       orderTotal: 20.9305, 
       orders:[ 
        { 
         id:1, 
         product: 'shirts', 
         total: 10.9484 
        }, 
        { 
         id:2, 
         product: 't-shirts', 
         total: 9.9821 
        } 
       ] 
      } 
     ]; 

的index.html

<!doctype html> 

    <html ng-app="myApp"> 
     <head> 
      <title>Routing Demo</title> 
      <link type="text/css" href="css/styles.css" rel='stylesheet'/> 
     </head> 
     <body> 
      <div ng-view></div> 

      <script type="text/javascript" src='scripts/angular.js'></script> 
      <script src="scripts/angular-route.js"></script> 
      <script type="text/javascript" src='app/app.js'></script> 
      <script type="text/javascript" src='app/services/customersService.js'></script> 
      <script type="text/javascript" src='app/controllers/customerCtrl.js'></script> 
      <script type="text/javascript" src='app/controllers/ordersCtrl.js'></script> 
      <script type="text/javascript" src='app/services/values.js'></script> 


     </body> 
    </html> 

請指導我在哪裏,我錯了。我對此很陌生,所以請幫助。

發佈的目錄結構 enter image description here

+0

那麼你的項目的目錄結構看起來像解決朝核問題? –

回答

1

的PIC服務器靜態文件,你需要添加:

app.use(express.static('public')); 

documentation描述。

當然,您可以使用除'public'以外的任何其他目錄,具體取決於您的靜態文件所在的位置。

+0

我是否需要指定我的文件的完整路徑? –

+0

問題解決了。我遵循你的建議,但把'public'改爲'__dirname' –

0

感謝您的幫助。我能夠通過增加線app.use(express.static(__dirname,'/'))並再次將其更改爲app.use(express.static(__dirname))

感謝

相關問題