Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Problems with expressjs on Nodester

Problems with expressjs on Nodester

Problem

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:

  1. 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!

  2. 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?

Problem courtesy of: fatmatto

Solution

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.

Solution courtesy of: Pavan Kumar Sunkara

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Problems with expressjs on Nodester

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×