mauricemach / zappa

Node development for the lazy.
zappajs.org
MIT License
952 stars 81 forks source link

set views dir to __dirname + '/views' by default #71

Closed rachel-carvalho closed 13 years ago

rachel-carvalho commented 13 years ago

When running an app from a different directory, it seeks the views folder in cwd, instead of in the app dir:

~/testapp$ cd ..
~$ coffee testapp/app.coffee
   info  - socket.io started
Express server listening on port 3000 in development mode
Zappa 0.2.0beta2 orchestrating the show

failed to locate view "23e6cc70-3e20-4679-a9a9-c7560631ffcb/index", tried:
  - /home/user/views/23e6cc70-3e20-4679-a9a9-c7560631ffcb/index.coffee
  - /home/user/views/23e6cc70-3e20-4679-a9a9-c7560631ffcb/index/index.coffee
  - /home/user/views/23e6cc70-3e20-4679-a9a9-c7560631ffcb/../index/index.coffee

In order for this to work, we have to explicitly set the views dir in my app, like we'd do in an express app:

require('zappa') ->
  set views: __dirname + '/views'
  get '/': ->
    render 'index'

It'd be nice if zappa could set this option by default.

neapel commented 13 years ago

Somehow my __dirname isn't my app's directory but the zappa@0.2.0 directory where npm installed it, i.e. running this

require('zappa') -> console.log __dirname

in /tmp doesn't print /tmp as I expected…

In plain node it works:

console.log(__dirname)
mauricemach commented 13 years ago

Inside a zappa block we lose access to __dirname and friends, which is one of the main reasons I'm thinking of moving to a @ based API in 0.3.0.

Meanwhile, we fake it by using module.parent.filename, which is the first file that required zappa in a process. It works in many common scenarios but is ultimately bound to fail.

Until 0.3.0 is released, you can use this if you're having problems:

require('zappa') {__dirname}, ->
  console.log __dirname
  set views: __dirname + '/views'