user375566
user375566

Reputation:

Loading Jade templates in various directories?

I am having trouble getting the results from this ticket to work. I am just trying to create more modular blocks of code, and want to put the routes and views all together. I need a sanity check on this one.

I can't get the route to pickup the Jade file using relative paths (and I dislike the relative paths): Error: Failed to lookup view "../test/app". Please help :)

Github: https://github.com/franklovecchio/stackoverflow-13770206

Folder structure:

    .
    │
    ├── package.json
    ├── public
    │   └── lib
    │       └── login
    │           ├── 0.0.1
    │           │   └── routes.coffee
    │           └── test
    │               └── app.jade 
    ├── server.coffee
    └── views
        └── layout.jade

server.coffee

express = require('express')
http = require('http')
path = require('path')

app = express()

app.configure () ->
  app.set 'port', process.env.PORT or 3000 # Give us the ability to specify port through command-line or external process.

  app.set 'view options', 
    layout: false

  app.set 'view engine', 'jade'

  app.use express.favicon() # auto-gen
  app.use express.logger('dev') # auto-gen
  app.use express.bodyParser() # auto-gen
  app.use express.methodOverride() # auto-gen

  # Required by session() middleware
  # Pass the secret for signed cookies
  app.use express.cookieParser('The Stay Puft Marshmallow Man')

  app.use app.router

  app.use express.static(path.join(__dirname, '/public')) # Serve static assets.

app.configure 'development', ->
  app.use express.errorHandler()
  app.use require('readymade').middleware(root: '/public') # Make .coffee/.less serve as compiled .js/.css files.

# Routes

# ======================  Login ======================  # 
login = require './public/lib/login/0.0.1/routes'
app.get '/', login.view
# ====================== /Login ======================  # 

http.createServer(app).listen app.get('port'), ->
  console.log 'Express server listening on port: ' + app.get('port')

routes.coffee

exports.view = (req, res) ->

  res.render '../test/app'

app.jade

extends ../../../../views/layout

p login

layout.jade

doctype 5
html
  head
    title My title
    block head
  body
    #content
      block content

package.json

{
  "name": "myapp",
  "description": "modular",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "express":"3.0.4",
    "less":"",
    "jade":"",
    "markitup":"",
    "readymade": ""
  }
}

Upvotes: 1

Views: 1305

Answers (2)

Juzer Ali
Juzer Ali

Reputation: 4167

It has to do with public directory being your static file serving directory. Remove the following lines from your server.coffee and it will work just fine.

app.use express.static(path.join(__dirname, '/public')) # Serve static assets.

Alternatively, put your code and views outside the public directory.

Upvotes: 1

etienne
etienne

Reputation: 3206

routes.coffee

exports.view = (req, res) ->
  res.render __dirname + '../test/app'

Upvotes: 0

Related Questions