typicallearner
typicallearner

Reputation: 256

How to run the content of another .js file?

Imagine I have two .js files, one app.js and the other one socket.js

the content of app.js is this:

const http = require("http");

const httpServer = http.createServer((_, res) => {
  res.write("Hello World!");
  res.end();
});

httpServer.listen(5000);

and my socket.js:

const io = require("socket.io").listen(6000);

io.on("connection", socket => {
  console.log("new connection!");
});

as you can see there's no need for me to use anything from socket.js inside app.js, but in order to actually run the content inside socket.js I need to do something like require('./socket.js') but it feels like it isn't the best solution for this. What's the best thing to do in these kind of situations? ( another example would be starting a database connection like Mongodb using mongoose when you don't need to export the database connection )

Upvotes: 0

Views: 36

Answers (2)

antonku
antonku

Reputation: 7665

It's perfectly fine to require a module without an assignment. For consistency you may want to launch both servers in the same app module though. This can be done if you export io server from the socket module:

const io = require("socket.io")();

io.on("connection", socket => {
    console.log("new connection!");
});

module.exports = io;

Then call .listen() on it in app.js:

const http = require("http");
const socketServer = require('./socket');

const httpServer = http.createServer((_, res) => {
    res.write("Hello World!");
    res.end();
});

httpServer.listen(5000);
socketServer.listen(6000);

Upvotes: 1

You are in fault here. You actually need something from that file, and that is the function that actually bootstraps socket.io, or mongoose in the other example you mentioned. When you run require('something') node, loads the file and starts executing the code in it, so the function is actually the whole file.

So, you could just use a require('socket.js') in your server.js file and get away with it ;) The general conclusion is that you don't need to export anything to use require.

What you are asking is impossible. You can't start two separate files in the same process with node. Node only reads one file, and then from the require's it proceeds to read the next files. The only way to do that is to have two node processes running... But that is a whole other issue that adds unnecessary complications, and can also be impossible to do in your app. So don't even try it :)

But a cleaner and more consistent pattern you could use, would be something like this:

In a file called server.js:

const http = require("http");

const httpServer = http.createServer((_, res) => {
  res.write("Hello World!");
  res.end();
});

function startServer(port = 5000) {
    httpServer.listen(port);
}

module.exports = startServer;

In a separate file called socket.js:

const io = require("socket.io");

io.on("connection", socket => {
  console.log("new connection!");
});

function startSocket(port = 6000) {
    io.listen(port);
}

module.exports = startSocket;

And then in your app.js:

const startServer = require('./server.js');
const startSocket = require('./socket.js');

startServer(5000);
startSocket(6000);

In this way you abstractify the bootstrap logic of your app, bootstraping each component separately, but allowing you to control the settings (eg: the ports) from one central module.

Upvotes: 1

Related Questions