I'm getting the following error :
/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.coffee:6
app.get('/login', function(req, res) {
^
ReferenceError: app is not defined
at Object.<anonymous> (/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.coffee:6:3)
at Object.<anonymous> (/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.coffee:15:4)
at Module._compile (module.js:449:26)
at Object.require.extensions..coffee (/Users/nblavoie/Desktop/HotPie/HotPie/node_modules/coffee-script/lib/coffee-script/coffee-script.js:22:21)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/Users/nblavoie/Desktop/HotPie/HotPie/app.js:31:1)
at Module._compile (module.js:449:26)
While trying to pass the (app) variable to the following require :
/**
* Module dependencies.
*/
require('coffee-script');
var express = require('express')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
// Routes
**require('./apps/authentication/routes')(app);**
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
Using the app variable in the following file :
routes = (app) ->
app.get '/login', (req, res) ->
res.render 'views/login',
title: 'Login'
stylesheet: 'login'
module.exports = routes
I know that the variable scope is different from one JS file to another. But why does the following line :
require('./apps/authentication/routes')(app);
Doesn't pass the app variable?
Right here:
ReferenceError: app is not defined
at Object.<anonymous> (/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.coffee:6:3)
The error isn't in app.js
, it's in your CoffeeScript file. Check out what it's compiling to:
var routes;
routes = function(app) {};
app.get('/login', function(req, res) {
return res.render('views/login', {
title: 'Login',
stylesheet: 'login'
});
});
module.exports = routes;
Notice that routes
is an empty function? Yep, you missed an indent. Changing your CoffeeScript to the following should fix it (notice the indentation of app.get
and everything after it):
routes = (app) ->
app.get '/login', (req, res) ->
res.render 'views/login',
title: 'Login',
stylesheet: 'login'
module.exports = routes
CoffeeScript is very sensitive to indentation. This code should work:
routes = (app) ->
app.get '/login', (req, res) ->
res.render 'views/login',
title: 'Login'
stylesheet: 'login'
module.exports = routes