I'm having an html file with a hyperlink which calls javascript function.The javascript function has to call a batch file...this all should happen from 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');
}
I'm getting error like this....
ReferenceError: require is not defined
var spawn = require('child_process').spawn,
any answer..pls reply...
You can't access Node.js from browser.
Node.js is a server-side environment for JavaScript. To interact with it from a web page, you'll want to establish an http.Server and use Ajax to communicate between.
A partial example (using a few libraries to simplify) would be:
// 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);
});
}
The libraries demonstrated are Express for server-side and jQuery for client-side. It also uses child_process.exec() rather than spawn() to get Buffers rather than Streams.
Resources:
node.js Tag Info, which includes a number of "Tutorials, Guides and Books" and "Free Node.js Books and Resources."