在新终端中启动带有child_process的Node.js Express服务器

时间:2016-01-04 06:50:16

标签: javascript node.js child-process

我正在寻找一种使用分离的child_process启动Node.js Express服务器的方法,我想将输出流式传输到新的终端窗口。 这有可能以某种方式与Node.js一起使用吗?

所以我有这个:

//server.js
var http = require('http');

const PORT=8080; 

function handleRequest(request, response){
    response.end('It Works!! Path Hit: ' + request.url);
}

http.createServer(handleRequest).listen(PORT, function(){
    console.log("Server listening on: http://localhost:%s", PORT);
});

然后我想通过运行此文件来启动该服务器

//start-server.js
var cp = require('child_process');
cp.fork('./server.js');

我想打开一个新终端,然后运行start-server.js文件......这可能吗?

1 个答案:

答案 0 :(得分:3)

var exec = require('child_process').exec,
child;

child = exec('gnome-terminal -x node',
function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
        console.log('exec error: ' + error);
    }
});

在node命令后添加file_name 它将始终在运行节点js

之前启动新终端

参考: - https://askubuntu.com/questions/401009/command-to-open-new-terminal-window-from-the-current-terminal/401012

https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

结合这两个想法