Node.js sending html file does not load related files (css, js)

I am trying to make az an ExtJS application with a Node.js server. Currently, the server code is as follows:

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); 

When I open localhost: 3000 in a browser, the html file loads, but not correctly. Validation in firebug I see that it cannot find related files in html. for instance

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

This is pretty logical since the file does not exist at this url. My question is how to fix this problem so that it can find every single related file in my file system.

I'm obviously doing something wrong or something is missing, I'm brand new to Node.

Thanks, advanced!

+4
source share
2 answers

It doesn't look like you're setting up an Express static file handler.

Try adding this code:

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

After var app = ... it will look like this:

 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); 

Then put your static files in the ./public directory.

+5
source

You will want to use some kind of static middleware, for example: http://www.senchalabs.org/connect/static.html

note express inherits the connection so you can

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

Or all:

 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); 
+3
source

All Articles