router.addRoute("[a-aA-z0-9]{1,50}.css$", function(matches){
var cssFile = matches[0];
var pathToCss = process.cwd() + "/" + cssFile;
// takes care of os diffs regarding path delimiters and such
pathToCss = path.normalize(pathToCss);
console.log(matches);
console.log("PATH TO CSS");
console.log(pathToCss)
var readable = fs.createReadStream(pathToCss);
var write = function(chunk){
this.queue(chunk.toString());
console.log(chunk.toString());
}
var end = function(){
this.queue(null);
}
var thru = through(write,end);
//req.on("end",function(){
res.pipe(readable.pipe(thru)).pipe(res);
//res.end();
//});
});
答案 0 :(得分:1)
您需要将可读流传输到整个流中,然后将其传输到响应中:
readable.pipe(thru).pipe(res);
编辑:为了准备你的css路径,只需使用path.join而不是连接你的路径并将其规范化:
var pathToCss = path.join(process.cwd(), cssFile);
答案 1 :(得分:0)
我从正常的html生成路由中分离出这条路径(css),我遇到的问题是我的路由器对象中的正常路由返回字符串,如res.end(compiled_html_str)
,并且css文件可读流正在通过相同的路由功能。我把它与我的路由器隔离开来。
var cssMatch = [];
if(cssMatch = req.url.match(/.+\/(.+\.css$)/)){
res.writeHead({"Content-Type":"text/css"});
var cssFile = cssMatch[1];
var pathToCss = process.cwd() + "/" + cssFile;
// takes care of os diffs regarding path delimiters and such
pathToCss = path.normalize(pathToCss);
console.log(cssMatch);
console.log("PATH TO CSS");
console.log(pathToCss)
var readable = fs.createReadStream(pathToCss);
var cssStr = "";
readable.on("data",function(chunk){
cssStr += chunk.toString();
});
readable.on("end",function(){
res.end(cssStr);
});
}