0

I am just going through nodejs, expressjs and coffeescript. My code is,

app.js

require('coffee-script').register();
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

//var index = require('./routes/index');
//var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//app.use('/', index);
//app.use('/users', users);
require('./apps/authentication/routes')(app);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

My CoffeeScript Code is.

routes.coffee

routes = (app) ->

app.get "/login", (req , res) ->
  res.render "views/login",
    title: 'Login'
    stylesheet: 'login'

module.export = routes

When i run project, i face following issue.

app.get("/login", function(req, res) {
  ^

ReferenceError: app is not defined

can anyone tell me why app variable is not defined at whereas i am passing app variable when requiring route?

If i remove white spaces from coffeescript file as mentioned below

routes = (app) ->
  app.get "/login", (req , res) ->
    res.render "views/login",
      title: 'Login'
      stylesheet: 'login'
module.export = routes

it return exception require(...) is not a function as mentioned below

require('./apps/authentication/routes')(app);
                                       ^

TypeError: require(...) is not a function

Thanks

1
  • Can you show the compiled javascript? I'm not much of a coffeescript expert, but if I recall correctly CoffeScript is whitespace sensitive, so the facts that routes = (app) -> and app.get … is on the same indentation level, means that app is not within the scope of the anonymous function. Commented Feb 16, 2017 at 14:02

1 Answer 1

2

Finally, I am able to fix this, it was syntax error in coffeescript.

I just changed

module.export

to

module.exports

Thanks,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.