Express / NodeJS
In this guide, we will discuss how to setup an Express project, that uses Onboardbase to manage its secrets(environment variables)
This section assumes you already have a project set up in Onboardbase Dashboard. If you don't, please create an account and get started!
Create an Express Project
We would be deploying the demo version of the express-generator-typescript
project.
From anywhere comfortable in your terminal, run:
// Using express-generator
npx express-generator-typescript "onboardbase-secrets"
cd "onboardbase-secrets" && npm run start:dev
Then, change into the project folder cd onboardbase-secrets
and add a remote git URL.
git remote add origin [REMOTE_GIT_URL]
git push --set-upstream origin main
git remote add origin [REMOTE_GIT_URL]
git push --set-upstream origin main
Finally, start the project with:
yarn start:dev
// or
npm start:dev
Install Onboardbase CLI
Follow the installation guide to set up Onboardbase CLI for your machine.
Verify installation with onboardbase --version
which would output the version of the CLI you just installed.
Authenticate Onboardbase CLI
From anywhere in your terminal, run onboardbase login
, and accept to open the page in a browser.
On the authorization page, enter your email, and a confirmation link would be sent to the email. Click on the link, and your CLI should be authorized. Check your terminal to confirm.
Setup Onboardbase CLI
After successful installation, from the Express Project directory in your terminal, run:
onboardbase setup
This would list all your projects, select the react project, select the development environment and accept to add .onboardbase.yaml to your .gitignore
file.
This would create a .onboardbase.yaml file that has all the details you selected during the setup and that would be used by the build script to know which secrets to pull into your react project.
Building with Onboardbase CLI
Since we have all our secrets on Onboardbase, we would be using the build tool from Onboardbase to load secrets into the project.
To do this, modify your start script inside the package.json
instead of having this:
"scripts": {
"build": "./node_modules/.bin/ts-node build.ts",
"lint": "eslint . --ext .ts",
"start": "node -r module-alias/register ./dist --env=production",
"start:dev": "nodemon",
"test": "nodemon --config ./spec/nodemon.json",
"test:no-reloading": "./node_modules/.bin/ts-node -r tsconfig-paths/register ./spec"
}
"scripts": {
"build": "./node_modules/.bin/ts-node build.ts",
"lint": "eslint . --ext .ts",
"start": "node -r module-alias/register ./dist --env=production",
"start:dev": "nodemon",
"test": "nodemon --config ./spec/nodemon.json",
"test:no-reloading": "./node_modules/.bin/ts-node -r tsconfig-paths/register ./spec"
}
To this:
"scripts": {
"build": "onboardbase run -c './node_modules/.bin/ts-node build.ts'",
"lint": "eslint . --ext .ts",
"start": "onboardbase run -c 'node -r module-alias/register ./dist --env=production'",
"start:dev": "onboardbase run -c 'nodemon'",
"test": "onboardbase run -c 'nodemon --config ./spec/nodemon.json'",
"test:no-reloading": "./node_modules/.bin/ts-node -r tsconfig-paths/register ./spec"
}
Notice how we now use onboardbase run
to run the start and build process, instead of the base express-scripts.
To test this, start the development server with:
yarn start:dev
Which should import all your projects from Onboardbase.
The secrets are available through: process.env.ENV_KEY_STORED_IN_ONBOARDBASE
Awesome!
Updated 9 months ago