olsibob

Can't load .env files in express.js server

Recommended Posts

I'm using express.js to serve a BabylonJS app statically. The dev server is ts-node running on localhost. Everything is going great except that it won't load .env files.

If I try loading the .env file:

const envTexture = BABYLON.CubeTexture.CreateFromPrefilteredData("assets/textures/mono_lake.env", scene);

const skyBox = scene.createDefaultSkybox(envTexture, true, 128);

I get the error "Cannot load cubemap because files were not defined" in the console.

Elsewhere on the forum, I see that people using IIS servers need to explicitly allow .env file mimetypes in a web.config file, but I'm wondering if this is necessary in express running on locahost?

What makes me think it is not a mime issue, is that I do seem to be able to load the .env as a  binary file if I use the code below:

Someone must be serving Babylon files over node, does anyone know how to get this working?

const assetManager = new BABYLON.AssetsManager(scene);
const skyboxTask = assetManager.addBinaryFileTask("skybox", "assets/textures/mono_lake.env");
skyboxTask.onError = (task, message, exception) => {
console.log(message);
}
skyboxTask.onSuccess = task => {
console.log("success, length: ", task.data.byteLength);// returns the correct length of the .env file
 
}
assetManager.load();
 
 

Share this post


Link to post
Share on other sites

I use expressjs and koa a lot for development.  You may be having trouble with binary being sent as text.  so, use res.send(..).  If the static file path is different, you can overrride it like this:
https://github.com/expressjs/express/blob/master/examples/downloads/index.js
If your network profile shows the file being sent then you need to provide your expressjs config. (ie: static).

Share this post


Link to post
Share on other sites

Checking the network conditioner, when I run the "binaryFileTask" operation, these are the headers in for the .env file:

    1. Request URL:
      http://0.0.0.0:2657/assets/textures/mono_lake.env
    2. Request Method:
      GET
    3. Status Code:
      200 OK
    4. Remote Address:
      0.0.0.0:2657
    5. Referrer Policy:
      no-referrer-when-downgrade
  1. Response Headersview source
    1. Connection:
      keep-alive
    2. Content-Length:
      1067838
    3. Content-Type:
      application/octet-stream
    4. Date:
      Thu, 23 Aug 2018 20:13:49 GMT
    5. ETag:
      W/"104b3e-UkXMdunGN93+cNYg5Su/4I5pzrg"
    6. X-Powered-By:
      Express
  2. Request Headersview source
    1. Accept:
      */*
    2. Accept-Encoding:
      gzip, deflate
    3. Accept-Language:
      en-GB,en-US;q=0.9,en;q=0.8
    4. Cache-Control:
      no-cache
    5. Connection:
      keep-alive
    6. Host:
      0.0.0.0:2657
    7. Pragma:
      no-cache
    8. Referer:
      http://0.0.0.0:2657/
    9. User-Agent:
      Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Edited by olsibob

Share this post


Link to post
Share on other sites

But, when I use the CubeTexture.CreateFromPrefilteredData code, I don't see the file at all in the network conditioner.

6 minutes ago, brianzinn said:

you may need to force binary by using Buffer().

Could you explain a little more how I would do this?

Share this post


Link to post
Share on other sites

In the node server, I try intercepting the request for the file, like this:

app.get('/assets/textures/:file(*)', (req, res, next) => {
    console.log(req.url, req.params.file);
    const staticPath = path.join(__dirname, "..", "..", "client", "dist", req.path);
    fs.readFile(staticPath, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ message: "oh no!", error: err });
        } else {
            const file = path.basename(req.path);
            res.contentType(file);
            console.log("sending");
            res.send(data);
        }
        res.end();
    });
});

This gets called and the logging works for png files etc, but it doesn't get called at all for the .env file (this is when the console for the client errors with "Cannot load cubemap because files were not defined"). It's as if the request for the .env file isn't being made at all

 

Share this post


Link to post
Share on other sites

@Deltakosh sorry about that. I'm struggling actually to get npm/webpack to send babylon-max instead of the minified one. I upload the source and then the build happens on the server, so I can't just manually copy the max file across.

edit: I think I've thought of a way to do this, I'll try again tomorrow.

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.