2

在本地运行此Express 应用程序有效在Heroku上运行它会在请求/或请求时给出粘贴错误/nested

Error: Failed to lookup view "index"
    at Function.app.render (/app/node_modules/express/lib/application.js:493:17)
    at ServerResponse.res.render (/app/node_modules/express/lib/response.js:753:7)
    at /app/server.js:21:16
    at callbacks (/app/node_modules/express/lib/router/index.js:161:37)
    at param (/app/node_modules/express/lib/router/index.js:135:11)
    at pass (/app/node_modules/express/lib/router/index.js:142:5)
    at Router._dispatch (/app/node_modules/express/lib/router/index.js:170:5)
    at Object.router (/app/node_modules/express/lib/router/index.js:33:10)
    at next (/app/node_modules/express/node_modules/connect/lib/proto.js:199:15)
    at store.get.next (/app/node_modules/express/node_modules/connect/lib/middleware/session.js:309:9)

Error: Failed to lookup view "nested/index"
    at Function.app.render (/app/node_modules/express/lib/application.js:493:17)
    at ServerResponse.res.render (/app/node_modules/express/lib/response.js:753:7)
    at /app/server.js:24:16
    at callbacks (/app/node_modules/express/lib/router/index.js:161:37)
    at param (/app/node_modules/express/lib/router/index.js:135:11)
    at pass (/app/node_modules/express/lib/router/index.js:142:5)
    at Router._dispatch (/app/node_modules/express/lib/router/index.js:170:5)
    at Object.router (/app/node_modules/express/lib/router/index.js:33:10)
    at next (/app/node_modules/express/node_modules/connect/lib/proto.js:199:15)
    at store.get.next (/app/node_modules/express/node_modules/connect/lib/middleware/session.js:309:9)

在我的服务器文件中:

express = require 'express'
app = express()

app.configure () ->

  app.set 'views', __dirname + '/views'
  app.set 'view engine', 'jade'

  app.use express.logger('dev')
  app.use express.bodyParser()
  app.use express.methodOverride()
  app.use express.cookieParser()
  app.use express.session(
    secret: 'WTF'
  )
  app.use app.router
  app.use express['static'](process.cwd() + '/public')

  app.get '/', (req, res) ->

    res.render 'index'

  app.get '/nested', (req, res) ->

    res.render 'nested/index'

port = process.env.PORT || 3000

app.listen port, () -> 
  console.log 'Server started on ' + port + '\nPress CTRL-C to stop server.'

在本地,我不需要指定app.set 'views', __dirname + '/views'服务器来运行和查找文件——不管有没有这个,它都不能在 Heroku 上工作。

4

1 回答 1

3

关于您的编辑:

问题可能是个案。虽然字符串指定小写路径:

这些文件实际上是大写的:

您的本地系统似乎使用了不区分大小写的文件系统,而 Heroku 没有。



为了帮助 Express 找到您的视图,您可以设置'views'根路径:

app.set('views', __dirname + '/views');

您也可以将绝对路径传递给res.render()

res.render(__dirname + '/views/admin/login');

另请注意,您可能不需要(也可能不想)通过req和:resres.render()

app.get('/admin/login', function (req, res) {
    res.render('admin/login');
});

有了它们, 的属性req将被包含locals在视图中,并且rescallback被忽略或导致错误,因为它不是function.

于 2013-02-12T18:03:22.313 回答