首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >管道连接杯流file.contents到服务器(连接、快速或http)响应

管道连接杯流file.contents到服务器(连接、快速或http)响应
EN

Stack Overflow用户
提问于 2014-05-21 18:30:40
回答 1查看 2.7K关注 0票数 4

我怀疑这是因为我对溪流的理解有限,但我已经到处寻找,无法让它发挥作用。简而言之,我想要获取一个Gulp流,并将流的连接内容直接传递给一个快速响应,而不需要写入文件系统。

我就是这样得到这个想法的(这个想法很好):

代码语言:javascript
复制
app.get('*', function(req, res){
    var stream = fs.createReadStream(__dirname + '/app/index.html');
    stream.pipe(res);
});

但是,我想使用湾流来应用相同的概念:

代码语言:javascript
复制
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时,它不起作用,并产生以下结果:

代码语言:javascript
复制
[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

这个错误是预料中的。吞咽被写入到输出到文件。

我想避免编写一个非常类似的插件。我可以提出建议,但就目前而言,是否有其他方法来实现这一目标?

谢谢!

这是完整的代码,如果你想试试的话。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-13 12:24:29

gulp工作在虚拟文件对象的流上,而不是物理文件。因此,无论您给文件系统取什么名称,gulp-concat都不会写入它。但是,您仍然会遇到问题,因为您无法将这些文件对象直接发送到res响应。

您需要将虚拟文件的内容写入res。一种简单的方法是使用through创建一个流,该流读取gulp输入并将文件的内容写入res。如果流处理多个文件,则不需要concat

代码语言:javascript
复制
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读取文件,因此如果不需要其他gulp,则可以直接使用乙烯基-fs。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23791136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档