Setting Up a NodeJs Server
Setting Up Onboardbase In A NodeJs Server Application
In this tutorial, we will show how you can use Onboardbase to manage the environment variables your NodeJs Server Application depends on.
The most popular way of doing this in NodeJs is by creating a .env
file in your project code and then using a NodeJs package like dotenv
which will load the environment variables into the process.env
.
With a setup like this, if multiple people work on a project, they will need to each have a copy of the .env
file, and they must all be aware of updates made by any member of the team and update their local .env
files.
This becomes more difficult as the number of people working on the project increases. And since applications are going to run in different environments that require different environment variables throughout their life cycle - development, staging, or production environment, each environment must use the right environment variables. It will be disastrous if the production application uses the development environment variables.
Managing Environment Variables with Onboardbase
Onboardbase is an all-in-one SecretOps infrastructure for securing environment configs/secret credentials across all stages, from development to production. It's designed for modern dev teams with rapidly changing requirements in collaborative environments.
Let's see how we can use Onboardbase to manage the environment variables for a NodeJs application. We will create a very simple NodeJs Express application for the purpose of this tutorial
Create a new folder for the demo project and open up your terminal in that folder. And run the following commands sequentially:
yarn init -y
yarn add express
After executing the commands, create an index.js
file with the following content:
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("GET request to the homepage");
});
const port = process.env.PORT;
if (!port) {
throw Error("You must set a Port for the App to Run");
}
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
This simple Express server depends on the PORT
environment variable to be available on process.env.PORT
before the server can start.
The current folder structure should look like this:
Try to run the project by executing
node index.js
This should fail since the PORT
environment variable is not available to the NodeJS process
Setting Up An Onboardbase Project
You must have installed the Onboardbase CLI and Signed Up with us to continue with this section.
After you are signed in to Onboardbase, you need to create a Project if you don't have one already.
Click Create your first project
, it should bring up this page where you can fill in information about your new project. Click Create
when you are done filling in the project details
The next step is to set up your environments and your environment variables:
We will be sticking with the development
environment name to start with, we can add more environments later on. Click on Variables
to add environment variables:
I have added a PORT
environment variable and my value is 8080
. The values are hidden by default, but you can click on show
to reveal them. Click Save
to save the environment variable.
After saving, you should be taken to the project overview page:
You can see the environment we have currently and the variables in it.
Let's now setup up Onboardbase CLI in the codebase.
Still in the terminal, run onboardbase login
. Select Yes
and a browser tab will open for you to login in.
After you log in on the browser, you should be authenticated on the CLI.
Run onboardbase setup
which will list all the Projects
you have on Onboardbase
After selecting a Project, you are also prompted to set an environment:
After selecting the development
environment, you should have the following view
A new file is also added to the project source by the Onboardbase CLI
Now execute
onboardbase run 'node index.js'
This should output:
Yay
We are now managing our environment variables using Onboardbase
If you have a start script that starts your application, you can pass that to onboardbase run
instead.
To demonstrate this, I have added this start script to my package.json
...
"scripts": {
"start": "node index.js"
},
...
Now I can execute onboardbase run 'yarn start'
to start the application
Conclusion
This tutorial shows how Onboardbase can help us manage the environment and environment variables that a NodeJs Application requires.
We also mentioned that Onboardbase makes it easy for teams to collaborate on projects. To share environment configs among team members, we need to add them to the Onboardbase Project, and they will be able to access the configs for that project.
Updated 26 days ago