.NET服务将* .gz文件导出到nodejs服务器。这些文件是gziped json字符串。
这是用于在本地保存文件的节点路由。
router.post("/", function (req, res) {
var filePath = path.join(__dirname, "../public", "data", req.header("filename"));
var writeStream = fs.createWriteStream(filePath);
req.pipe(writeStream);
req.on("end", function () {
writeStream.close();
res.sendStatus(200);
});
});
现在我的公共/数据目录中有.gz文件。
我从客户端js请求一个文件:
static fetchJsonFile(path:string, callback:Function):void {
let httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
let data = JSON.parse(httpRequest.response);
if (callback)
callback("OK", data);
}
else if (httpRequest.status === 404) {
if (callback)
callback("Resource not found", null);
}
}
};
httpRequest.open("GET", path, true);
httpRequest.responseType = "json";
httpRequest.send();
}
它不起作用 - 返回httpRequest.response == null。但是,服务器日志显示该文件已提供:GET /data/EURUSD1440.gz 200 1.352 ms - 322044
编辑当我将httpRequest.responseType
设置为"text"
时,httpRequest.response
会收到压缩后的文件,无法解析。
如何让客户端读取,解压缩和解析gzip压缩文件?
我尝试了很多变种没有成功。我的一些尝试:
使用Connect和connect-gzip-static:
connect().use(gzipStatic(path.join(__dirname, "public")));
fs
中的routes/data.js
:
router.get("/", function (req, res) {
var filePath = path.join(__dirname, "../public", "data", req.header("filename"));
console.log(filePath);
var readStream = fs.createReadStream(filePath);
res.setHeader("Content-Type", "application/json");
res.setHeader("Content-Encoding", "gzip");
res.setHeader("Vary", "Accept-Encoding");
res.setHeader("Content-Disposition","gzip");
readStream.on("open", function () {
readStream.pipe(res);
});
readStream.on("end", function() {
readStream.close();
console.log("there will be no more data.");
});
readStream.on("error", function(err) {
res.end(err);
});
});
不幸的是,我没有成功。有什么想法吗?
修改
看起来我无法正确设置响应标头以确保内容被gzip压缩。我的服务器主循环看起来像这样:
"use strict";
var debug = require("debug")("Ea_Studio:server");
var http = require("http");
var express = require("express");
var path = require("path");
var favicon = require("serve-favicon");
var logger = require("morgan");
var bodyParser = require("body-parser");
var fs = require("fs");
var routes = require("./routes/index");
var data = require("./routes/data");
var app = express();
app.use(logger("dev"));
app.use(favicon(path.join(__dirname, "public", "favicon.ico")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, "public")));
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");
app.use("/", routes);
app.use("/data", data);
看起来get请求不受routes/data.js
答案 0 :(得分:0)
我设法使用serve-static
模块修复它。它允许我为gzip
路由设置public/data
标头。
app.use(serveStatic(__dirname + "/public/data", {setHeaders: setDataFilesHeaders}));
function setDataFilesHeaders(res) {
res.setHeader("Content-Type", "application/json");
res.setHeader("Content-Encoding", "gzip");
res.setHeader("Content-Disposition", "gzip");
}
app.use(serveStatic(__dirname + "/public"));