# Using Node.js with NubesGen
This documentation is for running Node.js applications with NubesGen, and there is another options that might interest you:
- As Node.js applications can be packaged with Docker, you can also run them as Docker applications with NubesGen.
NubesGen supports creating Azure App Service instances and Azure Functions instances, depending on the type of Node.js application that you which to deploy.
# Tutorial: running a Node.js application with NubesGen
Tip: You can go to https://aka.ms/nubesgen-azure-shell (opens new window) to have those prerequisites installed, and run the script from a Web browser.
- Bash (opens new window), which is installed by default on most Linux distributions and on Mac OS X. If you're using Windows, one solution is to use WSL (opens new window).
- Azure CLI (opens new window). To login, use
- (optional) GitHub CLI (opens new window). To login, use
gh auth login.
- Create a sample Node.js Web application using NestJs (opens new window).
We'll follow the beginning of the NestJs "first steps" guide (opens new window):Select the default package manager (
npm i -g @nestjs/cli nest new nodejs-sample-app cd nodejs-sample-app
npm). In the project, open the
src/main.tsfile, and use the
$PORTenvironment variable to bind your application to the correct port:Open
await app.listen(process.env.PORT || 3000);
package.jsonfile, and add the
filesentry to tell NPM how to package your app:
"files": [ "dist" ]
- Create a project on GitHub called
nodejs-sample-app, and push the generated project to that repository. Change
<your-github-account>by the name of your GitHub account:
git init git add . git commit -m "first commit" git remote add origin https://github.com/<your-github-account>/nodejs-sample-app.git git branch -M main git push -u origin main
- In the cloned project (
cd nodejs-sample-app), set up GitOps with NubesGen by running the NubesGen CLI (more information here):
- Use the command-line with NubesGen (more information here) to generate a NubesGen configuration:
curl "https://nubesgen.com/demo.tgz?runtime=nodejs&application=app_service.standard&gitops=true" | tar -xzvf -
- Create a new branch called
env-dev, and push your code:
git checkout -b env-dev git add . git commit -m 'Configure GitOps with NubesGen' git push --set-upstream origin env-dev
- Go to your GitHub project, and check that the GitHub Action is running.
- You can go to the Azure Portal (opens new window) to check the created resources.
- The application should be deployed on your App Service instance. Its URL should be in the form
https://app-demo-XXXX-XXXX-XXXX-XXXX-dev-001.azurewebsites.net/, and you can also find it in the GitHub Action workflow (Job: "manage-infrastructure", step "Apply Terraform"), or in the Azure portal. As it is a simple application, it should print by default
- Once you have finished, you should clean up your resources:
- Delete the resource group that was created by NubesGen to host your resources, which is named
- Delete the storage account used to store your Terraform state, in the
# Which Azure resources are created
If you deploy your Node.js application to an Azure App Service instance, NubesGen will generate:
- An Azure App Service plan (opens new window) to define the type of App Service instance you will use.
- An Azure App Service instance (opens new window), configured to run Node.js code natively.
If you deploy your Node.js application to an Azure Function, NubesGen will generate:
- An Azure App Service plan (opens new window) to define the type of Azure Functions instance you will use.
- An Azure Functions instance (opens new window), configured to run Node.js code natively.
- An Azure Storage Account (opens new window), to store your Node.js application.
# Configuration options
In the generated
terraform/modules/app-service/main.tf file, NubesGen will configure some variables
for your application.
site_config block, NubesGen generates the following configuration:
app_command_line = "npm run start:prod"
This command is the one used to run the Node.js application. By default, this command
npm run start:prod, which is the default with NestJS (opens new window), but this
should be specifically configured depending on the framework used.
PORT environment variable
Azure App Service automatically assigns the
PORT variable, so your Node.js application
can listen to the correct port.
You need to configure it in your application:
app.listen(process.env.PORT || 3000);
# Other options
NubesGen will configure some environment variables for your application.
DATABASE_URL: the URL to your database
DATABASE_USERNAME: the database user name
DATABASE_PASSWORD: the database password
REDIS_HOST: the Redis host name
REDIS_PASSWORD: the Redis password
REDIS_PORT: the Redis port (by default
AZURE_STORAGE_ACCOUNT_NAME: the storage account name
AZURE_STORAGE_ACCOUNT_KEY: the storage account key
AZURE_STORAGE_BLOB_ENDPOINT: the blob storage endpoint
MONGODB_DATABASE: the MongoDB database name
MONGODB_URI: the MongoDB database URL