Skip to main content

Application Setup

Let's start by creating a new project, installing required dependencies, and setting up a basic server application.

Create project

Create a new folder for your project, navigate to it in the command line, and initialize a new Node.js project:

npm init -y

Next, install all the Node.js dependencies we're going to use. In this case it will be the Express.js framework, an Express.js middleware for handling cookie-based sessions, and finally the Forge SDK:

npm install --save express cookie-session forge-apis

The "dependencies" in your package.json file should now look something like this (potentially with slightly different version numbers):

// ...
"dependencies": {
"cookie-session": "^1.4.0",
"express": "^4.17.1",
"forge-apis": "^0.8.6"
},
// ...

Finally, let's create a couple more subfolders in your project folder that we're going to need later:

  • wwwroot - this is where we're going to put all the client side assets (HTML, CSS, JavaScript, images, etc.)
  • routes - this is where we're going to implement all the server endpoints
  • services - here we're going to keep all the server-side logic that can be shared by different endpoints

Application config

Our application will need a couple of configuration parameters to run properly, for example, the credentials of our Forge app for communicating with Autodesk Forge services, or the callback URL where our users will be redirected to during the 3-legged authentication workflow. We will pass these parameters to the server app using environment variables.

Create a config.js file in the root of your project folder, and add the following code:

config.js
let { FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, FORGE_CALLBACK_URL, SERVER_SESSION_SECRET, PORT } = process.env;
if (!FORGE_CLIENT_ID || !FORGE_CLIENT_SECRET || !FORGE_CALLBACK_URL || !SERVER_SESSION_SECRET) {
console.warn('Missing some of the environment variables.');
process.exit(1);
}
const INTERNAL_TOKEN_SCOPES = ['data:read'];
const PUBLIC_TOKEN_SCOPES = ['viewables:read'];
PORT = PORT || 8080;

module.exports = {
FORGE_CLIENT_ID,
FORGE_CLIENT_SECRET,
FORGE_CALLBACK_URL,
SERVER_SESSION_SECRET,
INTERNAL_TOKEN_SCOPES,
PUBLIC_TOKEN_SCOPES,
PORT
};

We simply read the environment variables from process.env, and exit the application immediately if any of the required properties are missing.

Now, to pass actual configuration values to our application for debugging purposes, we need to create a launch configuration. Use Run > Add Configuration in the menu to create a new configuration, and when prompted for the specific environment, choose Node.js. This will create a new .vscode subfolder in your project with a launch.json file where you can define different launch configurations. Replace the content of the file with the following:

.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Server",
"runtimeExecutable": "npm",
"runtimeArgs": [
"start"
],
"envFile": "${workspaceFolder}/.env",
"skipFiles": [
"<node_internals>/**/*.js"
]
}
]
}

We are defining a single launch configuration called Launch Server that will start our application (using the npm start command), and what is more important, it will look for a .env file in the project folder, and provide any <key>="<value>" pairs defined in this file as environment variables to our application. Let's create the .env file in the project folder, and populate it with our environment variables (using your own values instead of the placeholders of course):

FORGE_CLIENT_ID="your-client-id"
FORGE_CLIENT_SECRET="your-client-secret"
FORGE_CALLBACK_URL="http://localhost:8080/api/auth/callback"
SERVER_SESSION_SECRET="custom-encryption-phrase"
caution

Since the .env file contains sensitive information, make sure that it is not included in git! This can be ensured by adding the .env line to the .gitignore file.

Create basic server

Next we'll setup a basic server application.

Create a server.js file in the root of your project folder with the following code:

server.js
const express = require('express');
const session = require('cookie-session');
const { PORT, SERVER_SESSION_SECRET } = require('./config.js');

let app = express();
app.use(express.static('wwwroot'));
app.use(session({ secret: SERVER_SESSION_SECRET, maxAge: 24 * 60 * 60 * 1000 }));
app.listen(PORT, () => console.log(`Server listening on port ${PORT}...`));

For now the server isn't doing much, just serving client side assets from the wwwroot subfolder, and accessing session data stored in cookies. The cookies will be encrypted using a secret phrase that we will need to pass to the application via the environment variable SERVER_SESSION_SECRET.

Next, let's add a "start": "node server.js" script to the package.json file so that we can easily run our application later:

// ...
"scripts": {
"start": "node server.js"
}
// ...

Try it out

Start the application from Visual Studio Code (for example, via the Run > Start Debugging menu, or by pressing F5), and navigate to http://localhost:8080 in the browser. The server should respond with Cannot GET / because we haven't added any logic to it just yet. That's going to be the topic of the next step.