lui*_*sgo 4 javascript node.js express gulp
我怀疑这来自对溪流的有限理解,但我到处寻找,无法让它发挥作用.简而言之,我想获取Gulp流并将流的连接内容直接传递给快速响应,而无需写入文件系统.
这就是我的想法(它工作正常):
app.get('*', function(req, res){
var stream = fs.createReadStream(__dirname + '/app/index.html');
stream.pipe(res);
});
Run Code Online (Sandbox Code Playgroud)
但我想使用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');
});
Run Code Online (Sandbox Code Playgroud)
/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
Run Code Online (Sandbox Code Playgroud)
预计会出现这种错误.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
Run Code Online (Sandbox Code Playgroud)
gulp适用于虚拟File对象流,而不是物理文件.因此,gulp-concat无论您给出什么名称,都不会写入文件系统.但是,您仍然会遇到问题,因为您无法将这些文件对象直接发送到res响应.
您需要将虚拟文件的内容写入res.一种简单的方法是使用through一个流来读取gulp输入并将文件的内容写入res.如果您的流处理多个文件,那么您不需要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));
});
Run Code Online (Sandbox Code Playgroud)
此外,gulp内部用于vinyl-fs读取文件,因此如果没有其他需要,您可以直接使用vinyl-fs gulp.
| 归档时间: |
|
| 查看次数: |
2624 次 |
| 最近记录: |