2015-03-08 13 views
5

In nodejs mando un flusso di clienti in questo modo:Come leggere un flusso nodejs in angolare?

DockerService.get().pull('my/image', function(err, stream) { 
    if (err) { 
    return res.badRequest(err); 
    } else { 
    return stream.pipe(res); 
    } 
}); 

Questo viene attivato da un client angularjs utilizzando $ http.get:

pull: function() { 
    var url = ENV.apiEndpoint + '/docker/pull'; 
    var promise = $http.get(url).success(function(response) { 
    return response; 
    }).error(function(data, status) { 

    }); 
    return promise; 
} 

Tuttavia non riesco a leggere il flusso. Da angolare ricevo un messaggio di errore:

SyntaxError: Unexpected token { 
    at Object.parse (native) 
    at fromJson (file://build/vendor/angular/angular.js:1065:14) 
    at defaultHttpResponseTransform (file://build/vendor/angular/angular.js:8579:16) 
    at file://build/vendor/angular/angular.js:8664:12 
    at forEach (file://build/vendor/angular/angular.js:323:20) 
    at transformData (file://build/vendor/angular/angular.js:8663:3) 
    at transformResponse (file://build/vendor/angular/angular.js:9389:23) 
    at processQueue (file://build/vendor/angular/angular.js:13189:27) 
    at file://build/vendor/angular/angular.js:13205:27 
    at Scope.$get.Scope.$eval (file://build/vendor/angular/angular.js:14401:28) 

non riesco a trovare tutte le informazioni su come gestire i flussi in angolare. Mi puoi aiutare?

intestazioni:

enter image description here

La risposta è troppo grande per presentarlo qui ecco la prima parte di esso:

enter image description here

anteprima:

enter image description here

+0

Potete mostrare le intestazioni HTTP e il corpo che il codice server restituisce? La mia ipotesi è che tu abbia un'intestazione "Content-Type" non corrispondente per l'applicazione/json ma un corpo di testo in chiaro da "badRequest (err)". –

+0

@PeterLyons La richiesta get stessa restituisce 200 OK. Nella scheda di rete di Chrome posso vedere la risposta in pipe. Tuttavia in angolare viene attivato il callback dell'errore. La risposta è di tipo JSON. badRequest non viene chiamato affatto. –

+0

vedere qui se funziona per voi http: // StackOverflow.it/questions/26300350/handling-nodejs-streams-with-angularjs –

risposta

4

Quindi non è un JSON valido, è una serie di messaggi JSON delimitati da una nuova riga (presumibilmente). Questo è ciò che significa "il token imprevisto". Il parser integrato di Angular non supporta questo fuori dalla scatola. La domanda non riguarda realmente lo streaming rispetto ai dati bufferizzati, è solo un formato del corpo che angularjs non supporta direttamente. È necessario assicurarsi che il parser integrato di angular sia ignorato e quindi è possibile dividere la stringa su newline e provare/catturare l'analisi di ogni riga come oggetto JSON.

FYI, per quanto ne so, il modulo migliore per lo streaming nel browser è oboe.js. Potrebbe essere possibile utilizzare RonB/angular-oboe insieme a un codice per bufferizzare ciascuna riga, quindi analizzare la sintassi di ndjson in un flusso di oggetti JS.

1

tenta di aggiungere:

res.attachment() 

o

res.writeHead(200, {'Content-Type': data.ContentType }); 

forse problema c'è ...

nel codice, verrà simile a questo:

DockerService.get().pull('my/image', function(err, stream) { 
    if (err) { 
    return res.badRequest(err); 
    } else { 
    res.attachment('your_file.jpg'); // add file name here like req.files.name 
    return stream.pipe(res); 
    } 
}); 

o

DockerService.get().pull('my/image', function(err, stream) { 
    if (err) { 
    return res.badRequest(err); 
    } else { 
    res.writeHead(200, {'Content-Type': data.ContentType }); //switch data.ContentType to yours or get it from the file 
    return stream.pipe(res); 
    } 
}); 

a volte utilizzare res.attachment è buona idea, cos utilizza le intestazioni di destra

Problemi correlati