Jump to content

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


olsibob
 Share

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();
 
 
Link to comment
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).

Link to comment
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
Link to comment
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?

Link to comment
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

 

Link to comment
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.

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...