json - Javascript read gz files from node/express server -
a .net service export *.gz files nodejs server. files gziped json strings.
this node route saving files locally.
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); }); });
now have .gz files in public/data directory.
i request file client side js that:
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(); }
it doesn't work - returns httprequest.response == null. server log shows file served: get /data/eurusd1440.gz 200 1.352 ms - 322044
edit when set httprequest.responsetype
equal "text"
, httprequest.response
receives file compressed , cannot parsed.
how make client reading, decompressing , parsing gzipped files?
i tried many variant bit no success. of tries:
with connect , connect-gzip-static:
connect().use(gzipstatic(path.join(__dirname, "public")));
with fs
in 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 no more data."); }); readstream.on("error", function(err) { res.end(err); }); });
unfortunately, have no success. ideas?
edit
it looks cannot set response header correctly sate content gzipped. server main loop looks that:
"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);
it looks request not managed routes/data.js
i manage fix using serve-static
module. allows me set gzip
headers public/data
route.
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"));
Comments
Post a Comment