2014-05-21 1 views
4

이것은 스트림에 대한 제한된 이해에서 비롯된 것으로 의심되지만 사방에 쳐다 보았지만 제대로 작동하지 않습니다. 간단히 말해 Gulp 스트림을 가져 와서 스트림의 연결된 내용을 직접 명시 적 응답에 전달하고 파일 시스템에 쓰지 않고 싶습니다. 파이프 연결 Gulp 스트림 file.contents를 서버 (connect, express 또는 http) 응답에 연결

내가 (잘 작동) 아이디어를 가지고하는 방법입니다

app.get('*', function(req, res){ 
    var stream = fs.createReadStream(__dirname + '/app/index.html'); 
    stream.pipe(res); 
}); 

을하지만 나는 꿀꺽 스트림을 사용하여 동일한 개념을 적용 할 :

app.get('/app/js/concatenated-js-files.js', function(req, res){ 
    gulp.src('app/js/**/*.js') 
     .pipe(concat()) 
     .pipe(res); 
}); 
app.listen(5555, function() { 
    console.log('Listening on port 5555'); 
}); 

작동 및 수율하지 않는 브라우저에서 /app/js/concatenated-js-files.js을 요청할 때 다음을 입력하십시오.

[gulp] Error in plugin 'gulp-concat': Missing fileName option for gulp-concat 
    at module.exports (/Users/lgomez/Projects/index-packager/node_modules/gulp-concat/index.js:10:24) 
    at Object.handle (/Users/lgomez/Projects/index-packager/index.js:83:15) 
    at next_layer (/Users/lgomez/Projects/index-packager/node_modules/express/lib/router/route.js:103:13) 
    at Route.dispatch (/Users/lgomez/Projects/index-packager/node_modules/express/lib/router/route.js:107:5) 
    at /Users/lgomez/Projects/index-packager/node_modules/express/lib/router/index.js:213:24 
    at Function.proto.process_params (/Users/lgomez/Projects/index-packager/node_modules/express/lib/router/index.js:284:12) 
    at next (/Users/lgomez/Projects/index-packager/node_modules/express/lib/router/index.js:207:19) 
    at Layer.expressInit [as handle] (/Users/lgomez/Projects/index-packager/node_modules/express/lib/middleware/init.js:23:5) 
    at trim_prefix (/Users/lgomez/Projects/index-packager/node_modules/express/lib/router/index.js:255:15) 
    at /Users/lgomez/Projects/index-packager/node_modules/express/lib/router/index.js:216:9 

해당 오류는 expec입니다. 테드. gulp-concat은 (는) 파일에 출력되었습니다.

gulp-concat와 매우 유사한 gulp 플러그인을 작성하는 것을 피하고 싶습니다. 내가 포크를 잡고 그것을 제안 할 수도 있지만, 현재로서는 이것을 달성 할 수있는 또 다른 방법이 있습니까?

감사합니다.

시도해 보려면 전체 코드를 참조하십시오. 가상 파일 객체가 아닌 실제 파일의 스트림에

var express = require('express'); 
var gulp = require('gulp'); 
var concat = require('gulp-concat'); 
var app  = express(); 
app.get('/app/js/concatenated-js-files.js', function(req, res){ 
    gulp.src('app/js/**/*.js') 
     .pipe(concat()) 
     .pipe(res); 
}); 
app.listen(5555, function() { 
    console.log('Listening on port 5555'); 
}); 
// http://localhost:5555/app/js/concatenated-js-files.js 
+0

** 참고 : ** [이벤트 스트림] (https://github.com/dominictarr/event-stream), [bufferstreams] (https://github.com/nfroidure/BufferStreams) 및 [kat] (https://github.com/fent/node-kat)하지만 행운은 없습니다. – luisgo

답변

5

gulp 작품. 따라서 gulp-concat은 사용자가 지정한 이름과 상관없이 파일 시스템에 쓰지 않습니다. 그러나 이러한 파일 개체를 직접 res 응답으로 보낼 수 없기 때문에 문제가 계속 발생합니다.

내용을 (res)으로 작성해야합니다. 이를 수행하는 쉬운 방법은 through을 사용하여 꿀꺽 꿀꺽 한 입력을 읽고 파일의 내용을 res에 쓰는 스트림을 만드는 것입니다. 스트림에서 여러 파일을 처리하는 경우 concat이 필요하지 않습니다. gulp에 대한 다른 필요가없는 경우는 비닐-FS 직접 사용할 수 있도록

var through = require('through'); 

// create a stream that reads gulp File objects and outputs their contents 
function sendTo(res) { 
    return through(
     function write(data) { // this will be called once for each file 
      res.write(data.contents); 
     }, 
     function end() { // this will be called when there are no more files 
      res.end() 
     } 
    ); 
} 

app.get('/app/js/concatenated-js-files.js', function(req, res){ 
    gulp.src('app/js/**/*.js') 
     .pipe(sendTo(res)); 
}); 

또한, gulp는 내부적으로 파일을 읽을 vinyl-fs를 사용합니다.

+1

고맙습니다. 이것은 스트림으로 작업하는 것을 배우는 부분이므로 꿀꺽 꿀꺽 사용하지 않아도되지만 미니 맵을 사용하여 파일을 선택하는 방법과 미니 맵 선택자 배열을 사용하는 것이 매우 편리합니다. AFAIK Glob은 그렇게 할 수 없습니다. 하지만 고마워! 이거 훌륭해. – luisgo

+0

그냥 의사가 요구 한 것, 감사합니다! –