res.sendfile() doesn't serve javascripts well res.sendfile() doesn't serve javascripts well express express

res.sendfile() doesn't serve javascripts well


I guess this might help you...

in app.js file...

app.use(app.router);app.use(express.static(path.join(__dirname, 'public')));app.use("/styles",  express.static(__dirname + '/public/stylesheets'));app.use("/scripts", express.static(__dirname + '/public/javascripts'));app.use("/images",  express.static(__dirname + '/public/images'));// development onlyif ('development' == app.get('env')) {  app.use(express.errorHandler());}app.get('/', function (req, res) {      res.sendfile(__dirname + '/public/home.html');});

save the home.html inside the /public folder and JavaScript files in /public/javascripts, images in /public/images, css files in /public/stylesheets folder.

In the HTML file reference should be the words you define(eg: /scripts/home.js)... like this

    <link rel="stylesheet"   type="text/css" href="/styles/home.css" >    <script src="/scripts/home.js" type="text/javascript"></script>   


var express=require("express");var app=express();app.use('/', express.static(__dirname + '/website/views/'));app.set("views",__dirname+'/website/views');app.get("/",function(req,res){    res.sendfile('index.html');});

the codes above is mine.i wish to help you.


Why not something like this?

if(req.url == '/') { // Root lookups appear to be mapped to index.html    next();} else {    fname = [disk location of your website] + req.url;    fs.open(fname, 'r', function(err, fd) {        if(err) {            next();        } else {            fs.close(fd);            res.sendfile(fname);        }     });}