1

I am building a chat application, and this index.js file is in the "routes" directory. The error is on the first line. There is also an index.jade file in the "views" directory.

module.exports.index = index; module.exports.logon = logon; module.exports.message = message; exports.index = function index(req, res){ res.render('index'); }; function logon(req, res){ res.send('Logon'); }; function message(req, res){ res.send('Message'); }; 
1
  • You don't ever create a variable named index. Commented May 4, 2015 at 3:54

2 Answers 2

2

You are doing exports two times, also there is no variable index.

exports.index = function index(req, res){ res.render('index'); }; module.exports.index = index; 

try using

module.exports.index = index; var index = function(req,res) { res.render('index'); } 
Sign up to request clarification or add additional context in comments.

Comments

1

When you assign a function to a variable, or you immediately call a function, you turn it from a statement into an expression. This causes function hoisting to not happen. You can fix the issue by not assigning the function index to exports.index, to restore the hoisting behavior:

function index(req, res){ res.render('index'); }; 

You'll still have problem after that, because logon and message are not defined (you've defined login and chat instead).

1 Comment

Thanks. I'm following a tutorial. Before I had function index(req, res){ res.send('Index'); }; and then it said to change the index function to exports.index = function index(req, res){ res.render('index'); }; after creating an index.ejs file in the views directory, but I created an index.jade file instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.