11

I want to use static file serve without any rendering engine. I've tried to use:

res.sendfile('public/index.html');

on the '/' GET route, and express middleware for static files on my route:

app.use(express.static(path.join(__dirname, 'public')));

BUT it seems like all of the javascripts which the client asks for are downloaded with index.html file information.

How can I make a successful download of the CSS/JS static files ?

UPDATE:

Here is the route for the "res.sendfile ..." :

app.get('/*', index);

I want all of the requests to the server on any route will get index.html and all of its JS&CSS assosiciated with.

ohadinho
  • 6,894
  • 16
  • 71
  • 124

5 Answers5

18

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 only
if ('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>   
user2681045
  • 380
  • 1
  • 4
1
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.

user3543469
  • 198
  • 2
  • 6
0

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

Well, the simplest solution will be to move app.use(express.static(path.join(__dirname, 'public')))

up before you call app.use(app.router);

This way, the static middleware gets served before the app.get('/*', index);

Ladmerc
  • 1,158
  • 11
  • 18
-1

I've made an assumption here that your routes are declared in this order:

app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

If this is indeed the case, then the following suggestion holds:

The problem here is the app.get('/*', …) will intercept all requests that match, which is basically everything. Your static middleware won't get a chance at serving files.

If you remove that route, things should work for you as index.html is already in the public directory and can be served by the static middleware.

For a good explanation of how this works, see the answer to this question: node.js / express.js - How does app.router work?

Update based on additions to the above question:

You've stated this as the current behavior of your server:

it seems like all of the javascripts which the client asks for are downloaded with index.html file information.

You have asked the question:

How can I make a successful download of the CSS/JS static files ?

with this requirement

I want all of the requests to the server on any route will get index.html and all of its JS&CSS assosiciated with.

Your question and requirement are opposed to each other. The server will send back to the client exactly what you tell/configure it to. It will either always send back index.html which is exactly what your requirement states, or it will successfully serve up both index.html and any CSS/Javascript it references which is what your original problem statement was.

In one of your comments below you've stated:

the reason I want to do it, is because I'm using templates, and index.html wraps each template. I'm using angular on the client, and I'm starting to realize that i'll have to use a render engine in order to achieve this. Again, my angular client defines the partial url, and when it sends the request to : '/partial/sample' I need the index.html to wrap the 'sample.html' for instance

My assumptions based on this statement (please correct if wrong)

  • You are using client side templates
  • The files you are retrieving from the server are static (i.e., they need to be served up as is from the server)
  • Your routes are currently declared in this order
    1. app.use(app.router);
    2. app.use(express.static(path.join(__dirname, 'public')));
  • You are not doing any server side templating (i.e. everything is located under public somewhere)

If these assumptions are correct, the fix is to do what I originally suggested and remove this route:

app.get('/*', index);

If you do this (assuming your resources are referenced correctly):

  • your index.html will be retrieved as is from the server via the static middleware.
  • Each css/js file you've referenced in index.html will be returned from the server via the static middleware
  • Any requests to load template files (such as sample.html) will be serviced by your static middeware and returned to the client without modification
Community
  • 1
  • 1
dc5
  • 12,341
  • 2
  • 35
  • 47
  • Well, that depends on where you stick `app.use(app.router);` in your script. – deitch Aug 11 '13 at 04:09
  • Although I agree; I am not sure why you would want to do `res.sendfile('public/index.html')` since it is *already* in the public path. – deitch Aug 11 '13 at 04:10
  • @deitch good point. I'd made an assumption that may not be valid here. More code posted would definitely help diagnose the problem. – dc5 Aug 11 '13 at 04:13
  • @deitch the reason I want to do it, is because I'm using templates, and index.html wraps each template. I'm using angular on the client, and I'm starting to realize that i'll have to use a render engine in order to achieve this. Again, my angular client defines the partial url, and when it sends the request to : '/partial/sample' I need the index.html to wrap the 'sample.html' for instance.. – ohadinho Aug 11 '13 at 11:04