Second express.static not working online - node.js

I have my code locally working well, but when I pull my code on my digitalocean web server, my code doesn't work like locally.
app.configure(function(){
app.use(express.bodyParser());
app.use(express.static(__dirname + '/dynamicApp'));
app.use(express.static(__dirname + '/staticApp'));
});
When I try to access content in /staticApp it works locally, but it doesn't on my digital ocean server. Content in the /dynamicApp work properly.
Both environment have the same node.js version (0.10.17), I have loaded with Vim my server files, and they are the same. I have done ls in all folders, and the structure is the same. I tried in incognito mode in Chrome to see if it were caching issues, and the issue is still there.
Anyone has a clue why this could happen ?

try setting the paths that serve the static content like:
app.configure(function(){
app.use(express.bodyParser());
app.use('/dynamicApp', express.static(__dirname + '/dynamicApp'));
app.use('/staticApp', express.static(__dirname + '/staticApp'));
});

express 4.x , like this:
app.use('/public', express.static(__dirname+'/public'));
app.use('/data', express.static(__dirname+'/data'));

Related

How to make a virtual static path by express?

I'm trying to make a virtual static path using express but there was some problem I can't deal with.
That's what I found in doc of Express:
// Serve static content for the app from the “public” directory
// in the application directory:
// GET /style.css etc
app.use(express.static(__dirname + '/public'));
// Mount the middleware at “/static” to serve static content only when
// their request path is prefixed with “/static”:
// GET /static/style.css etc.
app.use('/static', express.static(__dirname + '/public'));
Here is the directory structures:
disk
static
css
js
img
index.html
server.js
If I just try the code below, nothing go wrong. And I can find the same directory structures in Sources option in Chrome DevTools;
app.use(express.static(__dirname + '/dist'));
But when I try to make a virtual directory using code below, it went wrong.
app.use('/music', express.static(__dirname + '/dist'));
My site can not show correctly and console show errors of getting static files .That's what i found in Chrome DevTools:
errors,
directory structures
I thought the directory structures should like this "music/static/css".
Can anyone tell me what's wrong. Thx!

Some JS and CSS assets not loading on Heroku, although loading locally

Locally, my node.js with express app works as expected. When I push to Heroku, the app is able to load some stylesheets and javascripts, but not others. Here is the relevant snippet from app.js:
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
Here are the assets that I'm loading from my jade templates:
link(href='/stylesheets/style.css', rel='stylesheet')
link(href='/lib/bootstrap/docs/assets/css/bootstrap.css', rel='stylesheet')
link(href='/lib/bootstrap/docs/assets/css/bootstrap-responsive.css', rel='stylesheet')
script(src='/lib/jquery/jquery-1.7.2.js')
script(src='/lib/bootstrap/docs/assets/js/bootstrap-collapse.js')
Everything works locally, but when I push to Heroku, the style.css file and the jquery file are loaded fine. The bootstrap files return a 404.
I'm baffled why this would work locally but would break, and in parts, on Heroku.
Check that the files are really present
Check if your static files are being served by node or the web server e.g. nginx. It is possible the web server is misconfigured to serve static files and is not looking in /lib/bootstrap/

Problems with expressjs on Nodester

i just started using nodester as nodejs application paas and i stepped into a couple of problems.
Let me clarify that my local machine runs node 0.7 while on nodester i'm using node 0.6.17
The following code is inside my server.js file, executed by the platform:
app.get('/static', function(req,res) {
res.sendfile('views/myFile.html',function(error){
if(err)
res.send('An error has occurred');
});
});
app.get('/', function(req,res){
res.render('index.jade');
});
The rest of the code is the code generated by Express.js
in particular the configuration is
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'jade');
app.set('view options', {layout: 'layout.jade'}); //added by me but with no results
If i run this configuration in my local machine, everything works fine, the '/' route, perfectly sends the index.jade view inside the proper layout.jade view.
The '/static' route, sends index.html without problems.
But if i run this code on nodester (after editing package.json and asking for node 0.6)
i get different results:
The '/' route doesn't render the layout.jade, but only index.jade. This is pretty weird, since i just edited the layout.jade file, generated by express!
The '/static' route just throws an error, that i can catch with the callback. So the html file is not sent.
Where am i wrong? i am probably missing something.. any ideas?
Answer for 2
In nodester the node process might be running from a different directory making process.cwd() not equal to your app's root directory.
To solve this issue, use the following code
app.get('/static', function(req,res) {
res.sendfile(__dirname + '/views/myFile.html',function(error){
if(err)
res.send('An error has occurred');
});
});
Answer for 1
Similiar problem as above. So, please check and tell me.

Expressjs not recognizing static files

I have a nodejs app and am using expressjs. I've defined my static directory, but when I access it, it doesn't load. My express config is:
var app = express.createServer().listen(8001);
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use('/public', express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.session({ secret: "appsession" }));
app.use(express.errorHandler({showStack: true, dumpExceptions: true}));
app.set('views', __dirname + '/views');
app.set('view engine', 'hbs');
});
Inside my /public directory I have 3 folders, css, js, and img. Inside css I have a style.css. When I try to access it directly via http://localhost:8001/public/css/style.css I get: Cannot GET /public/css/style.css
Any ideas what I could be doing wrong?
Thanks!
EDIT:
It seems to be related to how I have my routes setup. I'm doing it like this:
var routes = require('./routes')(db);
pp.get('/', routes.index);
Then in my index.js file, I have:
module.exports = function(db) {
return {
index: function(req, res, next) {
res.render('index');
}
}
}
I have my error handling enabled, but when I use the routing in this way, it doesn't use expresses error handling, however if I take this out, it does.
You setup the static http middleware as follows:
app.use(express.static(__dirname + '/public'));
And retrieve a file in ./public/css/style.css with the url:
"/css/style.css"
public is not part of the path when you actually request the file.
Change your static handler to this:
app.use('/public/css', express.static(__dirname + '/public/css'));
Then http://localhost:8001/public/css/style.css should get what you want
Full sample app that allows curl http://localhost:8001/public/css/style.css:
app.js
|-public
|-css
|-style.css
var express = require("express"),
app = express.createServer();
app.use('/public/css', express.static(__dirname + '/public/css'));
app.listen(8001);
Was running into the same issue found the answer here
https://github.com/senchalabs/connect/issues/298
When you have try to use nested files it kinda get lost,
it says fixed on the tracker a year ago, however i tried today and worked fine
I figured it out.
I have two services running on my host. Django is running the site at the root: http://myURL.com, and then Node is running at http://myURL.com/node
The configuration is fine with all the files in Node. The index.html file is requested fine, but the index.html when it requests the stylesheets and static files, the request gets caught by Django before it makes it to Node. Django saw the file and had no idea what it is and returned the 404 error.
By disabling Django from catching the requests to those files it all works fine.

Twitter Bootstrap LESS with Node.js & Express

Since Twitter Bootstrap 2 is out, I wanted to integrate that into my Node.js project. Unfortunately, there's something wrong with the less compiler and I can't get it to work. I put all files into the public folder and set up a new project with express -c less newproj. and added the lines for less
less = require('less');
app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
All Node tells me is:
Express server listening on port 3000 in development mode
undefined
On the client side I get a 500 (Internal Server Error) for the bootstrap.css file, which should be compiled by lessc.
lessc bootstrap.less
Works fine.
Anybody knows how to solve the issue?
For posterity, this has changed a lot in recent Express:
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(express.static(path.join(__dirname, 'public')));
// This is just boilerplate you should already have
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
Ok, here is what I have found for you.
First you need both the compiler and the static middleware. The compiler compiles your less and recompiles on changes, the static middleware does the actual serving of the css
app.use(express.compiler({ src : __dirname + '/public', enable: ['less']}));
app.use(express.static(__dirname + '/public'));
Second, for some reason when the compiler runs it is losing the current path information, so it can't find the includes. So I had to go through the bootstrap.css and add the path to each import.
#import "/public/stylesheets/reset.less";
This is clearly odd, I am going to dig into it further.
Edit: While odd, a deep look through the code shows me no simple way around it. A bit more searching found this pull request on the connect repo https://github.com/senchalabs/connect/pull/174 which offers a fix for this, but the devs don't seem to want it.
There are also some workarounds in that thread, but it seems the best idea is to absolute path your includes.

Resources