正在为所有请求返回索引文件

我对web开发相对较新。 我正在使用节点(与快递)和angularJS。 虽然解决scheme可能很简单,但我无法确定为什么每个请求的文件(css / javascript / html)的内容都与我的index.html文件的内容一起返回。

Chrome检查器的以下截图说明了这个问题:

controller.js文件的内容实际上是index.html文件的内容。

在这里输入图像说明

您可以从屏幕截图中的索引文件中查看相关内容。 以下是节点服务器代码:

/** * 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'); }; 

似乎每一个资源的请求是返回索引的内容(包括图像文件)。 任何想法或build议将不胜感激。

这是你的问题:

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

你想在其他app.get()的之前移动它。 基本上你已经告诉服务器在build立了一堆其他路由之后,用index.html来响应每个请求。