How to load .babylon.js files in Node.js with custom MIME type?

615 views Asked by At

Checking around the web, there are a number of hints, but no clear solutions on MIME type configuration to serve .babylon files exported from Blender, on Node.js.

Is anyone aware of a solution to this, thoughts on security concerns, or know a way to solve?

Thanks,

1

There are 1 answers

0
Nash Worth On

.babylon files exported by Blender can be loaded into Node.js with a dynamic MIME type setting for 'Content-type' on the response object. Also, the limited set of MIME types is likely a good practice for Node.

1) ADD Mime type into an array of all supported MIME types:

    var extensions = [".babylon" : "application/babylon"] 

2) Within Node createServer() handler:

    http.createServer(function(){...});

3) Initialize a dynamic mimeType variable from given file type:

    var fileName = path.basename(req.url) || 'index.html',
        ext = path.extname(fileName);
    var mimeType = extensions[ext];     

4) Populate response Content-type:

    fs.readFile(filePath,function(err,cont){
        if(!err){
            res.writeHead(200,{
                "Content-type" : mimeType
            });
            res.end(cont);
        }
    });

Example Node server available on github: https://github.com/anymscape/babyloninnode

After clone, with node installed, at root, type in command line: node server

Result: 3D in Node.js thanks to BabylonJS: http://babylonjs.com/