![](/img/trans.png)
[英]Why is `this` different when opening index.html in the browser as opposed to serving index.html with a node server?
[英]index.html not loading even when the node server is running successfully
我對AngularJS相當陌生,我使用$ http嘗試了一段新代碼。 我已經安裝了nodeJS並使用npm安裝了express。 當我在node- node server.js
server.js上運行server.js時,我得到了監聽消息。 但是,當我嘗試在瀏覽器中加載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>
請指導我哪里出問題了。 我對此並不陌生,請提供幫助。
要服務器靜態文件,您需要添加:
app.use(express.static('public'));
如文檔中所述。
當然,您可以使用除'public'
以外的任何其他目錄,這取決於您的靜態文件所在的位置。
謝謝您的幫助。 通過添加行app.use(express.static(__dirname,'/'))
並將其更改為app.use(express.static(__dirname))
我得以解決此問題
再次感謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.