3

Is it possible to make/use custom text commands in a batch file while running a nodejs server through it?

//Current batch file
  node nodeServer.js

//nodeServer.js
 function list(){
  //insert query
 }
 function unlist(){
  //delete query
 }

As of now, after i start the batch file, the nodeServer.js is started and the batch stops accepting any input.

I'd like to be able to type "nodeServer.js list"(in the batch window) and with that, call a function called "list" inside nodeServer.js,

I'm looking to insert data about the server into a database by running a insert query with the "list" function and run a delete query with nodeServer.js unlist to remove the inserted row before shutting down the server again.

I'm unfamiliar with batch files, Is this possible?

Update

To Clarify.. I want to type a text command IN the batch window, AFTER it have started the nodejs server, to run a specific function found inside the nodeServer.js

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
user2267175
  • 595
  • 5
  • 14
  • unclear? i'm using a batch file to run a nodejs server, i wanted to know if it is possible, and how, to make the batch call a function inside the nodejs server by typing a command like "nodeServer.js list" in the batch window – user2267175 Feb 28 '16 at 00:30
  • that "duplicate" is not even closely related.. – user2267175 Feb 28 '16 at 00:34
  • No, i'm looking to type a command, IN the batch file, AFTER it have started the nodejs server, to run a specific function found inside the nodeServer.js – user2267175 Feb 28 '16 at 00:37
  • I'm leaving this question and deleting all my comments because I still have no idea what you're asking. Since you now have gone 20 minutes with no answers at all and only one still-confused person to engage, that's probably a clue that your question is not very clear. – jfriend00 Feb 28 '16 at 00:40
  • i can't make it more clear than than.. i have a batch file which starts my node.js program, i then want to type a command Inside the batch window to run a function in my node.js program. – user2267175 Feb 28 '16 at 00:47
  • `i'm using a batch file to run a nodejs server` already makes no sense to me. I've never seen anyone do what you're trying to do with node. – Travis Webb Feb 28 '16 at 04:07
  • `i then want to type a command Inside the batch window`. I don't know what a "batch window" is. Are you looking for REPL? https://nodejs.org/api/repl.html – Travis Webb Feb 28 '16 at 04:12
  • @TravisWebb https://en.wikipedia.org/wiki/Batch_file Instead of starting the nodejs process through a CMD window, you can start it with a single click by using a .BAT file. – user2267175 Feb 28 '16 at 05:07

1 Answers1

0

You want to send command to the NodeJS after the node process started.

  • To start a command form NodeJS without pause the bat file use start
  • To send the command I will use a simple text file. I will write to the file from the batch using echo and read the file form NodeJS using watch, and readFileSync
  • I will support sending function name and arguments using spaces. for example: list a b c

The BAT file:

@echo off This is make the bat file to  not show the commands
REM `Rem` do nothing. It is exactly like // in javascript

REM This will start NodeJS without pause the bat
start node myNode.js

REM delete the command file if it exists
del command

REM Send 3 commands to the file. You can also add parameters
echo list >> command
echo list a b c>> command
echo unlist>> command

var fs = require('fs') var filename = __dirname + '/command'

// Add here any command you want to be run by the BAT file
var commands = {
    list: function() {
        console.log('list', arguments)
    },
    unlist: function() {
        console.log('unlist', arguments)
    }
}


console.log('watching:' + filename)
if (fs.existsSync(filename)) {
    console.log('File exists, running initial command')
    runCommand()
}
require('fs').watchFile(filename, runCommand)

function runCommand() {
    if(!fs.existsSync(filename)) return
    var lines = fs.readFileSync(filename, 'utf-8').split('\r\n')
    console.log(lines)
    fs.unlink(filename)
    for(var i=0;i<lines.length;i++){
        var line=lines[i]
        console.log(line)
        line=line.split(' ') // Split to command and arguments
        var command = line[0]
        var args = line.slice(1)
        if (!commands[command]) {
            console.log('Command not found:"' + command +'"')
            return;
        }
        console.log('Running command:', command, '(', args, ')')
        commands[command].call(null, args)
    }
}

Read more about FileSystem node Module: https://nodejs.org/docs/latest/api/fs.html#fs_class_fs_stats

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
  • Thank you for your answer, i'll look into it when i have time. – user2267175 Feb 28 '16 at 04:57
  • 1
    It is not so nice. I spent some time to write it for you. At least upvote, or accept the answer. – Aminadav Glickshtein Feb 28 '16 at 20:12
  • Hi, Amina, i will when i get a chance, haven't had time yet : / neck deep in some higher prio stuff – user2267175 Feb 28 '16 at 20:49
  • Thanks again for your answer, unfortunately, the .BAT doesn't work as intended, it starts the nodejs process, deletes & rewrites the command file immidiately, (all commands at once), then it closes the .BAT again and the nodejs process with it. – user2267175 Mar 01 '16 at 19:45