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.
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.
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…