Thanks Martin,
Returning function resolved the issue,
module.exports = function() {
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
return data;
});
ls.stderr.on('data', function (data) {
return data;
});
ls.on('exit', function (code) {
return code;
});
};
As my requirement is,
--> When a user click refresh button I want this function to execute and
get the data and load it on index page.
--> But I do not want, process to send the whole page back to server. So
that user don't have to wait until above function returns the data back to
client.
Hope this make sense, Please do suggest if the approch I explained above is
correct and I am in right track.
Thanks.
On Monday, February 11, 2013 10:52:50 AM UTC+11, Martin Cooper wrote:
>
> In your data.js file, you've got the code inside what would otherwise be
> an object literal. That's not valid JavaScript syntax. I'm not entirely
> sure what you want the module to look like, but perhaps you want to put the
> code inside a function and export that.
>
> --
> Martin Cooper
>
>
> On Sun, Feb 10, 2013 at 3:28 PM, Suraj Singh Thapa
> <[email protected]<javascript:>
> > wrote:
>
>> Thanks nicolas,
>>
>> I wanted to get the output of child_process to index page so,
>>
>> In *data.js*
>>
>> module.exports ={
>> var spawn = require('child_process').spawn,
>> ls = spawn('ls', ['-lh', '/usr']);
>>
>> ls.stdout.on('data', function (data) {
>> return data;
>> });
>>
>> ls.stderr.on('data', function (data) {
>> return data;
>> });
>>
>> ls.on('exit', function (code) {
>> return code;
>> });
>> };
>>
>> In *routes/index.js*
>>
>> var data = require('../data.js');
>> exports.index = function(req, res){
>> res.render('index', { title: 'Profiles', data: data});
>> };
>>
>> in *views/test.jade*
>> *
>> *
>> *ul#data*
>> *li #{data}*
>> *
>> *
>> I am not sure if this is the right approach to get the data to webpage
>> and even if its correct then I am getting following error,
>>
>> var spawn = require('child_process').spawn,
>> ^^^^^
>>
>>
>> SyntaxError: Unexpected identifier
>>
>>
>> Please suggest.
>>
>> Thanks
>>
>> On Saturday, February 9, 2013 4:30:02 PM UTC+11, nicolas wrote:
>>>
>>> Hello Suraj,
>>>
>>> I suspect that you have been using child_process.exec(). Look into
>>> child_process.spawn()<http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options>
>>> .
>>> It returns an EventEmitter and allows you to add stdout, stderr and exit
>>> listeners that will be called asynchronously. Here is an excerpt from the
>>> node.js doc on how to use spawn():
>>>
>>> var spawn = require('child_process').spawn**,
>>> ls = spawn('ls', ['-lh', '/usr']);
>>>
>>> ls.stdout.on('data', function (data) {
>>> console.log('stdout: ' + data);});
>>>
>>> ls.stderr.on('data', function (data) {
>>> console.log('stderr: ' + data);});
>>>
>>> ls.on('exit', function (code) {
>>> console.log('child process exited with code ' + code);});
>>>
>>>
>>> On Friday, February 8, 2013 6:01:30 PM UTC-8, Suraj Singh Thapa wrote:
>>>>
>>>> Hi,
>>>>
>>>> * I have been exploring Node, Express and chiild_process since last few
>>>> days. I have a static module with data (some array) which now I know how
>>>> to
>>>> show it to index page.
>>>>
>>>> * I can launch linux command and gets its output/error using
>>>> chile_process.
>>>>
>>>> Now I need to know, How can I constantly(Not to wait for process to
>>>> finish) get STDOUT of of command and pass it to index page.
>>>>
>>>> Any, suggestion would be helpful for beginners like me.
>>>>
>>>> Cheers,
>>>>
>>> --
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]<javascript:>
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.