javascript - How to run child_process on view Onclick event? -
javascript - How to run child_process on view Onclick event? -
i new node js , using express hogan or moustache templating views.
i can utilize below code in routing files, index.js below , works fine.
/* test shell execute. */ router.get('/shell', function(req, res){ exec('ls -1', function (error, stdout, stderr) { result = stdout.tostring().split("\n"); res.render('shell', { title: "shell", error: error, stderr: stderr, result: result }); }); }); what want able run , other exec command onclick events view.
when seek , utilize node require("child_process") in view, javascript undefined error. below.
class="lang-html prettyprint-override"><!doctype html> <html> <head> <script> window.onload= function() { var exec = require('child_process').exec; exec('ls -lrt', function (error, stdout, stderr) { }); }; </script> <title>{{ title }}</title> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> <h1>{{ title }}</h1> <p>hello {{ result }}</p> </body> </html> it seems tedious have run node code routing file , go , forth view. making big error here, need guidance.
am able run node code view? if not best way this?
console output:
referenceerror: require not defined shell:6 <!doctype html> <html> <head> <script> window.onload= function() { var exec = require('child_process').exec; exec('ls -lrt', function (error, stdout, stderr) {
following on comments;
logic should not placed in views, goes against whole mvc principle. logic on views seek executed on client, -i assume- not intention. as params beingness passed , forth, can -if using express, example- define middleware can take care of logic in on go. again, in express, havereq , res objects can attach like. should easy accomplish need. finally; can post more code can have improve thought of trying , might able create improve suggestion.
javascript node.js express child-process hogan.js
Comments
Post a Comment