-4

I am not very much familiar with nodejs but, I need some guidance in my task. Any help would be appreciated.

I have nodejs file which runs from command line.

filename arguments and that do some operation whatever arguments I have passed.

Now, I have html page and different options to select different operation. Based on selection, I can pass my parameters to any file. that can be any local node js file which calls my another nodejs file internally. Is that possible ? I am not sure about what would be my approach !

I always have to run different command from terminal to execute different task. so, my goal is to reduce that overhead. I can select options from UI and do operations through nodejs file.

Rishabh Shah
  • 541
  • 7
  • 28
  • Can we see some code @rishabh-shah? – Todd Dec 12 '14 at 18:24
  • 1
    @Todd I can not share that code. but, that does different operations and based on argument it do some task. – Rishabh Shah Dec 12 '14 at 18:28
  • Can anyone give simple example of such requirements ? well, I tried to use that nodefile. but, that does not work in browser as it says unexpected identified, etc. – Rishabh Shah Dec 12 '14 at 18:30
  • Please don't give downvote. Its important thing for me. – Rishabh Shah Dec 12 '14 at 18:31
  • 2
    the downvote wasn't me, man. It is *your* question, though, so the onus is on you to provide enough information for people to respond intelligently. – Todd Dec 12 '14 at 18:37
  • Are you saying you want to run shell commands from a browser? You can't do that. If you are just trying to simplify repeated commands, use [`alias`](https://www.digitalocean.com/community/tutorials/an-introduction-to-useful-bash-aliases-and-functions) – Evan Davis Dec 12 '14 at 18:46
  • 2
    @RishabhShah Without code people aren't really going to be able to help you. If you can't share it because it's private code then distill your problem down to a simple example and paste that code. What you're asking doesn't make much sense; we need more information. – CatDadCode Dec 12 '14 at 21:19
  • 2
    *"that can be any local node js file which calls my another nodejs file internally. Is that possible"* Yes. – Kevin B Dec 12 '14 at 22:40

1 Answers1

2

I was bored so I decided to try to answer this even though I'm not totally sure it's what you're asking. If you mean you just need to run a node script from a node web app and you normally run that script from the terminal, just require your script and run it programmatically.

Let's pretend this script you run looks like this:

// myscript.js

var task = process.argv[2];
if (!task) {
  console.log('Please provide a task.');
  return;
}

switch (task.toLowerCase()) {
  case 'task1':
    console.log('Performed Task 1');
    break;
  case 'task2':
    console.log('Performed Task 2');
    break;
  default:
    console.log('Unrecognized task.');
    break;
}

With that you'd normally do something like:

$ node myscript task1

Instead you could modify the script to look like this:

// Define our task logic as functions attached to exports.
// This allows our script to be required by other node apps.
exports.task1 = function () {
  console.log('Performed Task 1');
};

exports.task2 = function () {
  console.log('Performed Task 2');
};

// If process.argv has more than 2 items then we know
// this is running from the terminal and the third item
// is the task we want to run :)
if (process.argv.length > 2) {
  var task = process.argv[2];
  if (!task) {
    console.error('Please provide a task.'); 
    return;
  }

  // Check the 3rd command line argument. If it matches a
  // task name, invoke the related task function.
  if (exports.hasOwnProperty(task)) {
    exports[task]();
  } else {
    console.error('Unrecognized task.');
  }
}

Now you can run it from the terminal the same way:

$ node myscript task1

Or you can require it from an application, including a web application:

// app.js

var taskScript = require('./myscript.js');
taskScript.task1();
taskScript.task2();

Click the animated gif for a larger smoother version. Just remember that if a user invokes your task script from your web app via a button or something, the script will be running on the web server and not the user's local machine. That should be obvious but I thought I'd remind you anyway :)


EDIT

I already did the video so I'm not going to redo it, but I just discovered module.parent. The parent property is only populated if your script was loaded from another script via require. This is a better way to test if your script is being run directly from the terminal or not. The way I did it might have problems if you pass an argument in when you start your app.js file, such as --debug. It would try to run a task called "--debug" and then print out "Unrecognized task." to the console when you start your app.

I suggest changing this:

if (process.argv.length > 2) {

To this:

if (!module.parent) {

Reference: Can I know, in node.js, if my script is being run directly or being loaded by another script?

Community
  • 1
  • 1
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • 2
    Yes. I have same type of file which execute different operation based on arguments. Sorry for not posting code. but, I don't want to change js file (myScript.js in your case), but, I want to use/call that file nodejs, in your case (myscript.js), by any intermediate/js file which will be called from any action of UI in extjs/sencha. – Rishabh Shah Dec 15 '14 at 08:36
  • 2
    My nodejs file (myscript in your case), works fine from terminal based on argument we passed. but, same file I want to use. – Rishabh Shah Dec 15 '14 at 08:43
  • 1
    Yeah without anymore information I guess you're out of luck. I tried. – CatDadCode Dec 15 '14 at 18:22
  • 1
    Please tell me which input do you need from my side still ? – Rishabh Shah Dec 16 '14 at 05:00
  • 1
    We need code examples. You have to write code showing what you're trying to accomplish. It's the only way to help you. I don't understand why you can't obfuscate the code to avoid revealing anything about the project. Without some kind of code there's not much else anyone can do to help. Especially since English isn't your first language. – CatDadCode Dec 16 '14 at 18:11