La sintassi per lo spawn è:
spawn(<command>, [array of arguments]);
Ad esempio, eseguendo un ls comando con -l /home le opzioni sarebbero simili a questa:
ls = spawn('ls', ['-l', '/home'];
Quindi il tuo spawn('mongoexport',['--csv']); sta andando nella giusta direzione ma mongoexport --csv non è valido. Ecco perché stai ricevendo un errore. mongoexport ha bisogno di più di un semplice --csv . Come quello che hai fatto sopra, ad esempio, devi specificare il nome del database (-d "lms" ), nome della raccolta (-c "databases" ), nomi di campo (--fields firstname,lastname ), ed ecc.
Nel tuo caso, dovrebbe essere qualcosa del genere:
var spawn = require('child_process').spawn;
app.get('/export', function(req, res) {
var mongoExport = spawn('mongoexport', [
'--db', 'lms', '--collection', 'databases',
'--fields',
'firstname,lastname,email,daytimePhone,addressOne,city,state,postalCode,areaOfStudy,currentEducationLevel,company',
'--csv'
]);
res.set('Content-Type', 'text/plain');
mongoExport.stdout.on('data', function (data) {
if (data) {
// You can change or add something else here to the
// reponse if you like before returning it. Count
// number of entries returned by mongoexport for example
res.send(data.toString());
} else {
res.send('mongoexport returns no data');
}
});
}