F
F
f1nef1ne2018-10-17 14:42:29
Node.js
f1nef1ne, 2018-10-17 14:42:29

How to run multiple scripts in NodeJS?

Hello. It is necessary to launch the NodeJS server, which will simultaneously process API requests from the client, and in parallel, it is necessary to run a script that will run 1 time per second and make entries in the database. Is it possible to implement something like this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Shumov, 2018-10-17
@inoise

pm2, different ports and all

V
Victor L, 2018-10-17
@Fzero0

Well, try to organize everything in the form of modules

  • module1.js - server
  • module2.js - database entries
  • index.js - executable file
//module2.js
function module2() {
  console.log('module2 started);
  setTimeout(function () {
    setInterval(function () {
      console.log(('module2 timer:' + new Date().getTime()));
    }, 2000);
  }, 1000);
}

module.exports = module2;

//index.js
const module1 = require('./module1'),
          module2 = require('./module2');

module1();
module2();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question