在本地运行this 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,它都无法使用。
答案 0 :(得分:2)
关于你的编辑:
问题可能是个案。字符串指定小写路径:
文件实际上是大写的:
您的本地系统似乎使用不区分大小写的文件系统,而Heroku则没有。
要帮助快速查找您的观看次数,您可以设置'views'
根路径:
app.set('views', __dirname + '/views');
您也可以将绝对路径传递给res.render()
:
res.render(__dirname + '/views/admin/login');
另请注意,您可能不需要(也可能不想)将req
和res
传递给res.render()
:
app.get('/admin/login', function (req, res) {
res.render('admin/login');
});
使用它们,req
的属性将在locals
中包含在res
中callback
,因为function
将被忽略或导致错误,因为它不是{{1}}。