calling child_process from nodejs

calling child_process from nodejs

本文关键字:from nodejs process child calling      更新时间:2023-09-26

我有一个html文件,其中包含一个调用javascript函数的超链接。javascript函数必须调用一个批处理文件。。。这一切都应该在Node.js 中发生

<html>
<head>
<title>sample</title>
<script src="child.js"></script>
</head>
<body>
<a href="#" onclick="call()">click here</a>
</body>
</html>

child.js

function call()
{
var spawn = require('child_process').spawn,
ls = spawn('append.bat');
}

我犯了这样的错误。。。。

ReferenceError: require is not defined
var spawn = require('child_process').spawn,

任何答案。。请回复。。。

Node.js是一个用于JavaScript的服务器端环境。要从网页与之交互,您需要建立一个http.Server并使用Ajax在其间进行通信。

一个部分示例(使用一些库来简化)是:

// server-side
app.post('/append', function (req, res) {
    exec('appand.bat', function (err, stdout, stderr) {
        if (err || stderr.length) {
            res.send(500, arguments);
        } else {
            res.send(stdout);
        }
    });
});
// client-side
function call() {
    $.post('/append').done(function (ls) {
        console.log(ls);
    }).fail(function (xhr) {
        console.error(xhr.responseText);
    });
}

演示的库是Express用于服务器端,jQuery用于客户端。它还使用child_process.exec()而不是spawn()来获得Buffers而不是Streams。

资源:

  • 学习jQuery
  • 快速指南
  • SO的node.js标签信息,其中包括许多"教程、指南和书籍"answers"Free Node.js书籍和资源。"

您无法从浏览器访问Node.js。