Ich bin Anfänger in Web-Services und meine Kenntnisse sind nicht so tief in Knoten js, so entschuldige ich mich im Voraus, wenn die Frage nicht korrekt ist. Meine Frage ist, ich habe zwei Funktionen in meiner Angular-Node-Js-App. Erste Funktion, Datei auf öffentliche Ordner auf dem Server hochladen
var storage = multer.diskStorage({ //multers disk storage settings
destination: function (req, file, cb) {
cb(null, './demo/');
},
filename: function (req, file, cb) {
//var datetimestamp = Date.now();
cb(null, file.originalname
//file.fieldname + '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length -1]
);
}
});
var upload = multer({ //multer settings
storage: storage
}).single('file');
/** API path that will upload the files */
app.post('/upload', function(req, res) {
upload(req,res,function(err){
if(err){
res.json({error_code:1,err_desc:err});
return;
}
res.json({error_code:0,err_desc:null});
});
});
Second funciton, Executive Java-Anwendung
var child = function() {
spawn('java', ['-Xms64M', '-Xms64M', '-jar', '/home/ubuntu/code/ParseExcel.jar',
'/var/www/html/demo/test.xls']);
child.on('close', function (exitCode) {
if (exitCode !== 0) {
console.error('Something went wrong!');
}
});
child.stderr.on('data', function (data) {
process.stderr.write(data);
});
}
Aufruf Gibt es so etwas wie Winkel $ Uhr in Knoten js, dass ich es auf Datei-Upload festlegen könnte Funktion, so dass, wenn die Datei hochgeladen wurde erfolgreich Aufruf java Funktion
Lösung von @ Paul zur Verfügung gestellt (modifiziert)
app.post('/upload', function(req, res) {
upload(req,res,function(err){
if(err){
res.json({error_code:1,err_desc:err});
return;
}
// first, call your child code:
var child = spawn('java', ['-Xms64M', '-Xms64M', '-jar', '/home/ubuntu/code/ParseExcel.jar',
'/var/www/html/demo/test.xls']);
child.on('close', function (exitCode) {
if (exitCode !== 0) {
console.error('Something went wrong!');
}
});
child.stderr.on('data', function (data) {
process.stderr.write(data);
});
// In my case java app parsing xls to json around 5-8 sec, that's why I'm using timeout
setTimeout(10000);
// then respond to the client so it's not waiting:
res.json({error_code:0,err_desc:null});
});
//return req.child;
});
Das ist der Rückruf in Ihrer Upload-Middleware. wo haben Sie 'res.json ({error_code: 0, err_desc: null});' das ist der erfolgreiche Datei-Upload-Handler. – Paul
@Paul so statt 'res.json ({error_code: 0, err_desc: null});' Ich muss die Funktion '' child 'aufrufen, so etwas wie' res.child'? – Anton
Ich schreibe eine Antwort auf. – Paul