在各种目录中加载Jade模板?

时间:2012-12-07 20:02:29

标签: node.js pug express

我无法将this ticket的结果用于工作。我只是想创建更多的模块化代码块,并希望将路由和视图放在一起。我需要对这个进行健全性检查。

我无法使用相对路径获取Jade文件的路径(我不喜欢相对路径):Error: Failed to lookup view "../test/app"。请帮助:)

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

文件夹结构:

    .
    │
    ├── 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": ""
  }
}

2 个答案:

答案 0 :(得分:1)

它与public目录是静态文件服务目录有关。从server.coffee中删除以下行,它会正常工作。

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

或者,将您的代码和视图放在public目录之外。

答案 1 :(得分:0)

routes.coffee

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