Node.js发送html文件不会加载链接的文件(css,js)

我正在尝试使用Node.js服务器构build一个ExtJS应用程序。 我的服务器代码目前看起来像这样:

var express = require('express'); var app = express(); app.get('/', function (req, res) { res.sendfile(filedir + '/index.html'); }); app.get('/employees', function(req, res){ console.log("hello"); }); app.listen(3000); 

当我在浏览器中打开localhost:3000时,html文件被加载,但不正确。 检查萤火虫我看到它无法在HTML中find链接的文件。 例如

 "NetworkError: 404 Not Found - http://localhost:3000/ext-4/ext-debug.js". 

这是非常合乎逻辑的,因为该文件不存在于该URL上。 我的问题是如何解决这个问题,所以它可以find我的文件系统上的每一个链接的文件。

我显然做错了什么或缺less什么东西,我在节点上是全新的。

先谢谢了!

看起来不像你正在configurationExpress'静态文件处理程序。

尝试添加此代码:

 app.configure(function() { app.use(express.static(path.join(__dirname, 'public'))); app.use(express.bodyParser()); app.use(express.logger("short")); }); 

它将会在var app = ...像这样:

 var express = require('express'); var app = express(); app.configure(function() { app.use(express.static(path.join(__dirname, 'public'))); app.use(express.bodyParser()); app.use(express.logger("short")); }); app.get('/', function (req, res) { res.sendfile(filedir + '/index.html'); }); app.get('/employees', function(req, res){ console.log("hello"); }); app.listen(3000); 

然后将您的静态文件放在./public目录下。

您将需要使用一些静态中间件,例如: http : //www.senchalabs.org/connect/static.html

注意快递inheritance连接,所以你可以

 app.use(express.static(filedir)); 

或者完整的东西:

 var express = require('express'); var app = express(); app.use(express.static(filedir)); app.get('/employees', function(req, res){ console.log("hello"); res.send("hello"); }); app.listen(3000);