Node.JS

Qoddi automatically build your Node.JS apps created from the platform.
In order to do that Qoddi uses buildpacks to detect and build automatically your app.

Qoddi requires several files to be placed inside the root directory of your app to be able to build it :

  • package.json
  • At least one .js file (your code)

Optionally, you can add a Procfile file if you need to use a specific command.

You can fork or build this repository to see how the app creation process works.

Add-ons to run specific Frameworks

Qoddi developed specific add-ons to run popular NodeJs frameworks without the need to add a Procfile or to make any configuration changes.

Those add-ons automatically take care of everything specifically related to a framework including the Node environment variables required to run your app, the network settings, and the post-build scripts.

As of today, add-ons are available for :

NuxtJS

Gatsby

Create your app on FlashDrive

Create a new app and link it with your repository using SSH keys.

Read this article for a detailed procedure or check this video :

Default env variables

When FlashDrive deploys a Node.JS app, it automatically creates two environment variables: PORT and HOST

It’s critical to verify that those variables are used inside your code or that the values inside your code match the values of those environment variables.

To access env variables from your code you can use const PORT = process.env.PORT

Using the wrong port or host value will cause your app to crash to a 502 error.

Package.json

The mandatory file package.json is where the Node.JS settings of your app are declared. Heres’ an example :

{
  "name": "flashdrive_node_app",
  "version": "1.0.0",
  "description": "Node.js on FlashDrive",
  "author": "First Last <[email protected]>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

Please check this article for a detailled method on how to use your domain name with FlashDrive apps.

Specify a Node.JS version

Node versions adhere to semver, the semantic versioning convention popularized by GitHub. Semver uses a version scheme in the form MAJOR.MINOR.PATCH.

  • MAJOR denotes incompatible API changes
  • MINOR denotes added functionality in a backwards-compatible manner
  • PATCH denotes backwards-compatible bug fixes

To specify a Node.js version, set the engines.node in the package.json file to the semantic versioning specification (semver) range or the specific version of Node you are using.

Example showing a semver range:

"engines": {
  "node": "14.12.x"
}

Example showing a specific version:

"engines": {
  "node": "16.12.0"
}

If you try to use a version that is not currently supported, the build of your app will fail.

Currently, supported versions are 12.x14.x, and 16.x

Specify a NPM version

To specify an npm version, set engines.npm in the package.json file to the semantic versioning specification (semver) range or the specific version of npm you are using:

Example showing a semver range:

"engines": {
  "node": "14.12.x",
  "npm": "7.6.x"
}

Example showing a specific version:

"engines": {
  "node": "14.12.0",
  "npm": "7.6.3"
}

If you do not specify an npm version, your app uses the default npm packaged with the Node.js version used by your app, as specified on the Node.js releases page.

Do not include Dev Dependencies in your build

FlashDrive automatically builds devDependencies and Dependencies from your package.json file. The build is pruned before being sent to the production server and, in most cases, you should always use the default behavior as some packages are required for the build to complete.

If you want to prevent the builder to include devDependencies during the build phase, create an Environment Variable NODE_ENV with the value production and relaunch your build.

Using Yarn instead of NPM

By default, the Node.js buildpack assumes you are using NPM. If you want to use Yarn instead, you must provide a yarn.lock file in your top-level app directory. For more information on the Yarn lock file, see yarn.lock in the Yarn documentation.

Procfile

On node.JS, using a Procfile is not mandatory, you can use the start command inside the scripts part of the package.json file to declare what script is launched at startup. FlashDrive will build the app according to your settings and launch npm automatically.

The Procfile informs the engine of the type of app you are running (web or worker) and prepares the infrastructure accordingly. Make sure to use the same server type as during the app creation (web or worker) or you will need to manually alter the networks settings of the app.

The Procfile basically contains the type of app and the initial script to run when the app starts :

web: npm start

Customizing the Build process

If your app has a build step that you’d like to run when you deploy, you can use a build script in package.json:

"scripts": {
  "start": "node index.js",
  "build": "webpack"
}

Fix missing dependencies

If some dependencies declared inside package.json are not available directly the build can fail. To prevent that, add npm update && inside the build sequence of package.json :

"scripts": {
  "start": "node index.js",
  "build": "npm update && webpack"
}

Debug and Access Logs

The log page inside your App settings only provides limited logs and is erased after each app reboot or rebuild.

Visit this page to learn how to make logs persistent for NodeJS and access them at any time from your browser.

Create persistent storage

One of the major improvements of Qoddi in comparison to others app hosting solutions is the ability to create Virtual Disks available for any copy of your app inside the same stack.

Virtual disks allow you to store data safely and securely, without the need to use an additional S3 bucket, and every app size gets free disk space depending on its size (see our pricing for more information).

In addition to that, Virtual disks are backup every day with 10 days history and include ransomware protection at no additional cost.

Read this article for more information about virtual disks and how to add one to your app.

At the moment, NodeJS apps cannot write or access data into virtual disks because of the permissions associated with the “node” user running the app locally. Virtual disks can still be used for other tasks like log persistence.

Define environment variables

Note: Environment variables set in your app settings are injected during the build process. If the build of your app needs to use an env variable make sure to set it before requesting the build.

FlashDrive lets you externalize the configuration of your app : the Qoddi cluster will automatically injects the data when the app start or restart.

Environment variables can include any external data your app needs to run like external resources, databases addresses, encryption keys, etc…

To add an environment variable, access your app settings and click “Add ENV Variable” :

Enter the key (like DATABASE_USER) and the value and click “Add”. Qoddi will automatically add your environment variable and restart your app after a one-minute delay (to let you add more env variables if necessary without restarting the app each time a new variable is added).

Add a domain name

Please check this article for a detailed method on how to use your domain name with Qoddi apps.

Provision a database

Qoddi’s Marketplace includes all popular datastore engines including Mysql, Postgres, MongoDB, and Redis.
To add a database to your app, click on Marketplace and select the database you want to run, then create the new app database inside the same stack as your Node.JS app.

Once the app is running, visit the database app’s settings and retreive the “internal name” :

Use this internal name as you would use a server address, for instance for MySQL this name replaces the usual “localhost”. Some scripts require you to add the database port at the end of the server address. Qoddi uses a common port for each database software, for MySQL it will be 3306 and you can write it like that inside your scripts: internal-name:3306
Postgres usually uses credentials sent inside the login URL, like that : postgres://<user>:<password>@<internalname>:5432/<database>

The login and password information is available inside the Environment Variables of the database app.

Reach your app from another app

Inside your app stack, you can reach any other Qoddi app member of this stack on any port. Qoddi automatically routes the traffic internally and opens the appropriate port. By opposition, your app is only reachable on ports 80/443 from outside of Qoddi.

To reach your app internally, retrieve the internal name from your app settings page :

This name can be used to reach your app from another app inside the same stack. Use it in any form that works with your script :

http://<internalname>
<internalname>
http://internalname>:port

Note: https:// is not available, all the traffic inside apps is already encrypted by the cluster.

Encryption

Qoddi uses at rest encryption for the app virtual disks, build images, and encrypted transport from inside the cluster. To make sure the traffic is encrypted between your app and the browser of your users you can set up SSL certificates for any domain name connected to the app (including the FlashDrive’s default domain name).

Scale your App

From your app settings you can scale your app vertically and horizontally: by upgrading the app size (refer to our pricing for app sizes details) or by adding more nodes.

Additional nodes will run another copy of your app inside another Qoddi’s server located inside the same cluster (same geographical location). Traffic is automatically sent to the least occupied node by Qoddi’s load balancer.

Note: Apps that use Virtual Disks will share the same file structure and perform read/write operations on the same disk. It’s fine in most cases for intensive read activity but can create bottlenecks and corrupted data if several nodes of the same app write on the same file at the same time. If your apps use intensive reads on virtual disks consider upgrading the app size instead of adding more nodes.

Was this helpful?

3 / 0