Problem with CesiumTerrainProvider in NodeJS applications

Hi,

I got an error, when i tried to create instance of CesiumTerrainProvider in my node.js app. When i start my app here comes an error:
"An error occurred in "CesiumTerrainProvider": An error occurred while accessing https://assets.agi.com/stk-terrain/world/layer.json."
I installed CesiumJS via npm: npm install cesium.

There is code of my app:

(function () {

    var express = require('express');
    var compression = require('compression');
    var app = express();
    app.use(compression())

    var bodyParser = require('body-parser')
    app.use(bodyParser.json()); // to support JSON-encoded bodies
    app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
        extended: true
    }));

    var Cesium = require(‘cesium’);
    var terrainProvider = new Cesium.CesiumTerrainProvider({
        url: ‘https://assets.agi.com/stk-terrain/world’,
        requestVertexNormals: true
    });

    var underscore = require('underscore');

    console.log("Start");

    app.post('/', function (req, res) {
        var request = req.body, response = {};
        console.log("Requested positions", request.positions);
    });

    var server = app.listen(3000, function () {
        var port = server.address().port;
        console.log('Aegis-Server listening at port %s', port);
    });
       
})();

Changing url to http:// or // doesnt help.
Thanks,
Dann.

Unfortunately, not all Cesium objects work under Node (as Cesium is primarily a client-side browser library). In this case I believe the problem is the use of XmlHttpRequest (which does not exist in Node). We could detect this and fallback to the npm request module, but no one is actively working on that. We’d be happy to look at any pull requests that try and improve Node compatibility.

Thanks for fast response. I will try to improve Node compatibility of CesiumJS.

I know this is an old thread, but I am seeing the same error: "An error occurred in “CesiumTerrainProvider”: An error occurred while accessing http://assets.agi.com/stk-terrain/world/layer.json." when I instantiate new Cesium.CesiumTerrainProvider({url : ‘http://assets.agi.com/stk-terrain/world’}).

Is this still an issue or do you guys now have a workaround for this problem?

Thank you very much,

Ricardo

Hello Ricardo,

Thanks for finding this issue. I don’t believe anyone has worked on a workaround for the problem yet.

You could follow Matt’s suggestion and modify this function to use request if XmlHttpRequest doesn’t exist: https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Core/loadWithXhr.js#L137

See our Build Guide for instructions on checking out and building the code: https://github.com/AnalyticalGraphicsInc/cesium/tree/master/Documentation/Contributors/BuildGuide

Best,

Hannah

My current workaround is to use PhantomJS instead of node for this purpose. In order to enable PhantomJS xmlhttprequest to work with a remote service like this one, you need to invoke with command line options as follows: phantomjs --local-to-remote-url-access=true --web-security=false yoursampleterrainscript.js

When I get some time I can try and modify the function and submit a PR.

Thanks again,

Ricardo

Hi Hannah

I submitted a PR today for this issue. I hope it is acceptable to you.

Thank you,

Ricardo

This is a very old thread, but I experience similar issues as described here when running Cesium in a node app, especially since I “just want to use” the sampleTerrain function.
Is that issue ( XMLHttpRequest within Cesium functions, which don’t work in node apps) still existing?
Basically, all I want to do is create a CesiumTerrainProvider instance (using a local terrain tile server) and then run the sampleTerrain using that terrain provider…

II debugged a bit: maybe it is also a proxy issue… Is there a way to set proxy options for those “internal” Cesium calls?

Update:
I added an https-proxy-agent object as agent to the options object of the http request in the loadWithHttpRequestfunction of CesiumUnminified\index.cjs file.
That got this working (but for sure, is a modification to the Cesium code…