mapbox / pbf

A low-level, lightweight protocol buffers implementation in JavaScript.
BSD 3-Clause "New" or "Revised" License
801 stars 107 forks source link

I use nodejs request module, can not create a Pbf object #124

Closed houxieyu closed 4 years ago

houxieyu commented 4 years ago

Please help me!This is very important to me. My code is as below,when read a pbf file,success to create a pbf object,when request on web,failed to create.Same two files! How can I do?Help!

var fs = require('fs');
var VectorTile = require('@mapbox/vector-tile').VectorTile;
var request = require('request');
var rp = require('request-promise');

function filetest() {
    var data = fs.readFileSync('49.mvt');
    var tile = new VectorTile(new Protobuf(data));
    var layerNames = Object.keys(tile.layers);
    console.log('file tile layers:', layerNames);
}

function urltest() {
    rp({
        url: 'http://124.70.193.2:6082/49.mvt',
        method: "GET",
    }).then(function (res) {
        var pbf = new Protobuf(res);
        var tile = new VectorTile(pbf);
        var layerNames = Object.keys(tile.layers);
        console.log('url tile layers:', layerNames);
    });
}
urltest();
filetest();

below is output: file tile layers: [ 'xian', 'cun' ] url tile layers: []

mourner commented 4 years ago

Please don't use GitHub to ask generic development questions, there is StackOverflow for this. Request returns a string as response by default, you need to set encoding: null in the options probably.