2013-11-15 20 views
1

我對web開發相對較新。我正在使用節點(與快車)和角JS。雖然解決方案可能很簡單,但我無法確定爲什麼每個請求文件(css/javascript/html)的內容都與我的index.html文件的內容一起返回。爲所有請求返回索引文件

從Chrome檢查下面的截圖說明了問題:

什麼正在顯示的controller.js文件的內容實際上是index.html文件的內容。

enter image description here

您可以從屏幕截圖索引文件中看到相關內容。以下爲節點服務器代碼:

/** 
* Module dependencies. 
*/ 

var express = require('express'), 
    routes = require('./routes'), 
    api = require('./routes/api'); 

var app = module.exports = express(); 

// Configuration 
app.configure(function() { 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(express.static(__dirname + '/public')); 
    app.use(app.router); 
}); 

app.configure('development', function(){ 
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
}); 

app.configure('production', function(){ 
    app.use(express.errorHandler()); 
}); 

// Routes 
app.get('/', routes.index); 
app.get('/partials/:name', routes.partials); 

// JSON API 
app.get('/api/name', api.name); 

// redirect all others to the index (HTML5 history) 
app.get('*', routes.index); 

// Start server 
app.listen(3000, function(){ 
    console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env); 
}); 

的routes.js文件(路徑目錄):

/* 
* GET home page. 
*/ 

//get correct directory path 
var filePath = __dirname.replace('routes', 'views/') 

exports.index = function(req, res) { 
    res.sendfile(filePath + 'index.html'); 
}; 

exports.partials = function (req, res) { 
    var name = req.params.name; 
    res.sendfile(filePath + 'partials/' + name + '.html'); 
}; 

看來,對資源的每個請求與指數的返回內容(包括圖像文件)。任何想法或建議將不勝感激。

回答

1

這是你的問題:

// redirect all others to the index (HTML5 history) 
app.get('*', routes.index); 

你要移到了其他app.get()的面前。基本上你已經告訴服務器在設置了一堆其他路由之後用index.html來響應每個請求。

+0

嘿傑森,將這個catchall行移到'get'的頂部似乎沒有任何影響 - 索引文件仍然被返回。儘管如此,完全取決於404s而不是200s。這是否意味着我需要專門爲js/cs文件設置路線? – bornytm

+1

這絕對意味着你的文件不在正確的位置......你不應該爲每個文件設置一個路徑。你的公共目錄裏是否有公共目錄,否則index.html中的路徑中的公共是多餘的? –

+1

非常感謝您的幫助Jason-這是一個將文件放在正確位置(公共目錄內沒有公共目錄)的問題。 – bornytm