0

I hope you all are well. I am having trouble with displaying my own forge data in the AutoDesk Forge reference application. My current .env file is as follows. However, whenever I launch it in http://localhost:9000/upload all I get in return is a blank empty screen.

FORGE_CLIENT_ID=STEHw2Qx... marked ...xrIJUeKRj6 #changed for post
FORGE_CLIENT_SECRET=A54... marked ...c348a #changed for post
FORGE_ENV=AutodeskProduction
FORGE_API_URL=https://developer.api.autodesk.com
FORGE_CALLBACK_URL=http://localhost:9000/oauth/callback

FORGE_BUCKET=cosmostool1.cosmosengineering.es #changed for post
ENV=local

#ADAPTER_TYPE=local
## Connect to Azure IoTHub and Time Series Insights
# ADAPTER_TYPE=azure
# AZURE_IOT_HUB_CONNECTION_STRING=
# AZURE_TSI_ENV=
#
## Azure Service Principle
# AZURE_CLIENT_ID=
# AZURE_APPLICATION_SECRET=
#
## Path to Device Model configuration File
# DEVICE_MODEL_JSON=
## End - Connect to Azure IoTHub and Time Series Insights

ADAPTER_TYPE=csv
CSV_MODEL_JSON=server/gateways/synthetic-data/device-models.json
CSV_DEVICE_JSON=server/gateways/synthetic-data/devices.json
CSV_DATA_END=2011-02-20T13:51:10.511Z  #Format: YYYY-MM-DDTHH:MM:SS.000Z
CSV_DELIMITER="\t"
CSV_LINE_BREAK="\n"
CSV_TIMESTAMP_COLUMN="time"

if (process.env.ENV == "local") {
    require("dotenv").config({
        path: __dirname + "/../.env",
    });
}
Eason Kang
  • 6,155
  • 1
  • 7
  • 24
  • Why did you add `if(process.env.ENV == "local") ... ` in the env file ? This is the code used by the app to load the `.env` file and assign all variables you defined as environnement variables ! Remove this part. Is there any error in your web console when you have the black screen ? – AlexAR Jul 06 '22 at 09:36
  • @AlexAR it runs the same with and without it. and no, there is no error in the web console ? I ran it again without this portion of code with the same result – Wes Hinchman Jul 06 '22 at 09:46
  • Do you have a launch.json file? – Arrotech Jul 06 '22 at 09:53
  • @Arrotech no I have just been running it though the terminal with ENV=local npm run dev – Wes Hinchman Jul 06 '22 at 10:00
  • @WesHinchman - First rotate your client_secret ASAP, second never ever disclose your client credentials. – Madhukar Moogala Jul 06 '22 at 12:02
  • 1
    @MadhukarMoogala do you see the part where it says changed for post? – Wes Hinchman Jul 07 '22 at 07:32

2 Answers2

1

Because of this line at forge-dataviz-iot-reference-app/server/router/Index.js#L25, you must specify ENV=local before executing npm run dev. Otherwise, it won't read the content of .env.

if (process.env.ENV == "local") {
    require("dotenv").config({
        path: __dirname + "/../.env",
    });
}

Or you can just change it to the below

require("dotenv").config({
        path: __dirname + "/../.env",
});

enter image description here

Eason Kang
  • 6,155
  • 1
  • 7
  • 24
0

Install dotenv

npm install dotenv

Create a config.js file in your directory and add the following code;

const dotenv = require('dotenv');
dotenv.config();
module.exports = {
    // Set environment variables or hard-code here
    azure: {
        azure_conn_string: process.env.AZURE_IOT_HUB_EVENT_HUB_CONNECTION_STRING
    }
};

Update your localserver.js file

const { app, router } = require("./app.js");
const config = require('./config');
app.use(router);
const server = require("http").createServer(app);

if (config.azure.azure_conn_string) {
    require("./RealTimeApi.js").createSocketIOServer(server);
}

const PORT = process.env.PORT || 9000;

async function start() {
    try { server.listen(PORT, () => { console.log(`localhost: ${PORT}`); }); } catch (error) { console.log(error); }
} start();
Arrotech
  • 109
  • 5
  • So do I add the `{ ... "envFile": ".env", ... } const dotenv = require('dotenv'); dotenv.config();` to my .env file? @arrotech – Wes Hinchman Jul 06 '22 at 10:20
  • If you are using a launch.json then you should add envFile property to your launch.json, otherwise just install the dotenv and configure on your server.js file as show above. – Arrotech Jul 06 '22 at 10:23
  • Should I make a server.js file since the reference application doesn't already have one ? or am I missing it? @arrotech – Wes Hinchman Jul 06 '22 at 10:25
  • Not really server.js is the file that runs your nodejs server... you might have called it something else like start.js – Arrotech Jul 06 '22 at 10:31
  • is it called localserver.js ? I have that in the git cloned reference application @arrotech – Wes Hinchman Jul 06 '22 at 10:34
  • Share a screenshot so that I can tell – Arrotech Jul 06 '22 at 10:36
  • this is what the localserver.js file looks like `const { app, router } = require("./app.js"); app.use(router); const server = require("http").createServer(app); if (process.env.AZURE_IOT_HUB_EVENT_HUB_CONNECTION_STRING) { require("./RealTimeApi.js").createSocketIOServer(server); } const PORT = process.env.PORT || 9000; async function start() { try { server.listen(PORT, () => { console.log(`http://localhost:${PORT}); }); } catch (error) { console.log(error); } } start();` – Wes Hinchman Jul 06 '22 at 10:42
  • Yes, that is the file. Let me update the above code with your code. Check back in a few – Arrotech Jul 06 '22 at 10:43
  • Let me know if that one works – Arrotech Jul 06 '22 at 11:01
  • Which one are you referring to? @arrotech – Wes Hinchman Jul 06 '22 at 11:05
  • I have just updated my answer above. Follow the steps. – Arrotech Jul 06 '22 at 11:08
  • I followed these instructions, but when i went to launch it with `ENV=local npm run dev` it gave me the error `[2] [nodemon] app crashed - waiting for file changes before starting...` @arrotech – Wes Hinchman Jul 06 '22 at 11:34
  • You don't need to run it with ENV=local npm run dev. Just run it with npm start. kindly send me the link to this project so that I can test it out for you. – Arrotech Jul 07 '22 at 06:12
  • alright thank you. here is the github repository with all of the recommended changes made https://github.com/weshinchman/forge-dataviz-testrepo – Wes Hinchman Jul 07 '22 at 07:33
  • why does the config.js use Azure if my .env file doesn't? – Wes Hinchman Jul 11 '22 at 10:16
  • ^^^ thank you^^^ @arrotech – Wes Hinchman Jul 11 '22 at 12:00