Running ol3cesium using nodejs on desktop

I was trying to run ol3-cesium as a desktop application. I downloaded the latest code with examples from the official site : http://openlayers.org/ol3-cesium/.

When I tried opening the main.html (from examples folder) in my web browser..........


 And as soon as I hit the enable/disable button view the map in 3D looks like this.......



Debug and you will get an error similar to this:
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

How did I solve it?
Its a CORS issues and can be solved by using a web server. I used nodejs server to host the directory.

Create a server.js file in your ol3-cesium root directory.
The content of my server.js:

===========

var http = require('http');
var fs = require('fs');
var url = require('url');


// Create a server
http.createServer( function (request, response) {
   // Parse the request containing file name
   var pathname = url.parse(request.url).pathname;
 
 
   // Read the requested file content from file system
   fs.readFile(pathname.substr(1), function (err, data) {
      if (err) {
         console.log(err);
      }else{
console.log("Server responding");
         response.writeHead(200, {'Content-Type': 'text/html'});
         response.write(data.toString());
      }
      response.end();
   });
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');


===========

run server.js as
> node server.js

Server is running at http://127.0.0.1:8081/

You can now hit:
http://127.0.0.1:8081/examples/main.html

And...

Its now running from your file system.

Prerequisites:
1. Nodejs installed on your machine

PS: I have changed the raster layer in the example

Comments

Popular posts from this blog

Writing your own ejabberd Module

npm ECONNREFUSED error

Conditional Flow - Spring Batch Part 6