1

I want to make a little chat app for learning and I created many js files which for separate like modular but I got some error.

this is my boot.js file

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var admin = io.of('/app'),
 client = io.of('');
module.exports.app = app;
module.exports.http = http;
module.exports.io = io;

this is my routes.js file

module.exports = {
 start: function(app) {
 app.get('/', function(req, res) {
 res.sendFile(__dirname + '/index.html');
 });
 }
};

this is my chat.js

var boot = require('./boot');
require('./routes')(boot.app).start();
http.listen(3000, function(){
 console.log('listening on *:3000');
});

when i run the node i got this error

require('./routes')(boot.app).start();
 ^
TypeError: require(...) is not a function

how could I use the variables which in the boot.js to other js files?

help me please.

Arpit Solanki
9,9914 gold badges45 silver badges57 bronze badges
asked Jan 31, 2018 at 7:29
3
  • 1
    shouldn't it be require('./routes').start(boot.app); Commented Jan 31, 2018 at 7:31
  • Seems like you are doing too many things in a single line. require is not async. Make sure express is initialzed before you require it Commented Jan 31, 2018 at 7:32
  • works thank you so much :) Commented Jan 31, 2018 at 7:33

1 Answer 1

2

You are making a small mistake here. Below is the correct one.

require('./routes').start(boot.app);

routes module exports an object where you can call start and use that function.

answered Jan 31, 2018 at 7:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.