2016-02-28 43 views
2

在通過它運行nodejs服務器時,是否可以在批處理文件中創建/使用自定義文本命令?如何將BAT文件中的命令發送到Windows中正在運行的NodeJS進程?

//Current batch file 
    node nodeServer.js 

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

截至目前,i之後啓動批處理文件時,nodeServer.js開始並且將該批料停止接收任何輸入。

我希望能夠輸入「nodeServer.js名單」(在批處理窗口),並與,稱所謂的「名單」內nodeServer.js功能,

我期待通過使用「list」函數運行插入查詢並使用nodeServer.js unlist運行刪除查詢以在再次關閉服務器之前刪除插入的行,將有關服務器的數據插入到數據庫中。

我不熟悉批處理文件,這可能嗎?

更新

要澄清.. 我想在批處理窗口中鍵入文本命令,之後,它已經啓動了服務器的NodeJS,運行nodeServer.js內發現了一個特定的功能

+0

不清楚?我正在使用批處理文件來運行nodejs服務器,我想知道是否有可能,以及如何通過在批處理中鍵入一個類似「nodeServer.js list」的命令來使批處理調用Nodejs服務器中的一個函數窗口 – user2267175

+0

「重複」甚至沒有密切關聯.. – user2267175

+0

不,我正在尋找鍵入一個命令,在批處理文件後,它已經啓動nodejs服務器,運行一個特定的函數內部找到nodeServer.js – user2267175

回答

0

您想要在節點進程啓動後向NodeJS發送命令。

  • 要啓動命令的形式沒有的NodeJS暫停bat文件使用start
  • 要送我將使用一個簡單的文本文件中的命令。我將使用echo從批處理文件寫入文件,並使用watchreadFileSync
  • 讀取文件格式NodeJS我將支持使用空格發送函數名稱和參數。例如:list a b c

BAT文件:

@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 

變種FS =需要( 'FS') VAR文件名= __dirname + '/命令'

// 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) 
    } 
} 

瞭解更多關於FileSystem節點模塊:https://nodejs.org/docs/latest/api/fs.html#fs_class_fs_stats

+0

謝謝你的回答,我會在有空的時候看看它。 – user2267175

+0

這不是很好。我花了一些時間爲你寫。至少要積極投票或接受答案。 – Aminadav

+0

嗨,阿米娜,我會當我有機會,還沒有時間:/脖子深在一些更高的prio的東西 – user2267175

相關問題