在关注使用Coffeescript和Node.js的书籍示例时,我正在尝试在routes.coffee文件中定义路由,如下所示:
routes = (app) ->
app.get '/login', (req,res) ->
res.render "views/login",
title: 'Login'
stylesheet: 'login'
module.exports = routes
将app对象传递给主启动文件应用程序中的routes.coffee(或者它应该)。:
require('./routes')(app);
尝试启动应用程序(节点应用程序)时出现以下错误:
/Users/eugene/Documents/DevTraining/MeanStack/NodeJS/HotPie/routes.coffee:6
app.get('/login', function(req, res) {
^
ReferenceError: app is not defined
at Object.<anonymous>
(/Users/eugene/Documents/DevTraining/MeanStack/NodeJS/HotPie/routes.coffee:5:1)
at Object.<anonymous>
(/Users/eugene/Documents/DevTraining/MeanStack/NodeJS/HotPie/routes.coffee:2:1)
at Module._compile (module.js:456:26)
at Object.loadFile
(/Users/eugene/Documents/DevTraining/MeanStack/NodeJS/HotPie/node_modules/coffee-
script/lib/coffee-script/register.js:16:19)
at Module.load
(/Users/eugene/Documents/DevTraining/MeanStack/NodeJS/HotPie/node_modules/coffee-
script/lib/coffee-script/register.js:45:36)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/eugene/Documents/DevTraining/MeanStack/NodeJS/HotPie/app.js:34:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
请问这项工作的正确方法是什么?
答案 0 :(得分:0)
好吧,看来,在稍微弄乱了缩进后,app对象被识别出来了:
routes =(app) - &gt;
app.get '/login', (req,res) ->
res.render "views/login",
title: 'Login'
stylesheet: 'login'
module.exports = routes