简体   繁体   English

管道连接Gulp流file.contents到服务器(连接,表达或http)响应

[英]Piping concatenated Gulp stream file.contents to server (connect, express or http) response

I suspect this comes from a limited understanding of streams but I've looked everywhere and cannot get it to work. 我怀疑这来自对溪流的有限理解,但我到处寻找,无法让它发挥作用。 In short, I want to take a Gulp stream and pass the concatenated contents of the stream to an express response directly without writing to the file system. 简而言之,我想获取Gulp流并将流的连接内容直接传递给快速响应, 而无需写入文件系统。

This is how I got the idea (which works fine): 这就是我的想法(它工作正常):

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

But I want to apply the same concept using a Gulp stream: 但我想使用Gulp流应用相同的概念:

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');
});

Which does not work and yields the following when requesting /app/js/concatenated-js-files.js from the browser: 从浏览器请求/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

That error is expected. 预计会出现这种错误。 gulp-concat was written to output to file. gulp-concat被写入输出到文件。

I'd like to avoid writing a gulp plugin that would be very similar to gulp-concat. 我想避免编写一个与gulp-concat非常相似的gulp插件。 I may fork and suggest it but, for now, is there another way to achieve this? 我可能会分叉并提出建议但是,现在,还有另一种方法来实现这一目标吗?

Thank you! 谢谢!

Here's the complete code if you'd like to try it. 如果你想尝试一下,这是完整的代码。

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

gulp works on streams of virtual File objects, not physical files. gulp适用于虚拟File对象流,而不是物理文件。 As such, gulp-concat doesn't write to the file system no matter what name you give it. 因此,无论您给出什么名称, gulp-concat都不会写入文件系统。 However, you will still have a problem because you cannot send these file objects directly to the res response. 但是,您仍然会遇到问题,因为您无法将这些文件对象直接发送到res响应。

You need to write the contents of the virtual file(s) to res . 您需要将虚拟文件的内容写入res An easy way to do this is to use through to make a stream that reads the gulp input and writes the files' contents to res . 一种简单的方法是使用through来创建一个读取gulp输入的流并将文件的内容写入res If your stream handles multiple files, then you don't need concat . 如果您的流处理多个文件,那么您不需要concat

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));
});

Also, gulp internally uses vinyl-fs to read the files, so you can use vinyl-fs directly if have no other need for gulp . 另外, gulp内部使用vinyl-fs来读取文件,因此如果没有其他需要gulp ,你可以直接使用乙烯基gulp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM