Lecture Notes Of Class 29: Deploying Node.js Application
Objective:
Learn how to deploy a Node.js application to the cloud.
Introduction to Deployment
Deployment
is the process of making your application accessible to the world. For a
Node.js application, deploying means uploading the app's code to a server or
cloud platform where it can be accessed by users.
There
are several cloud platforms for deploying a Node.js application, and in this
class, we will focus on Heroku because it is simple to use and
has a free tier suitable for learning and small applications.
Why Deploy a Node.js Application?
1.
Make it Accessible: Deploying your
app makes it accessible over the internet, so others can interact with it.
2.
Cloud Benefits: By deploying to the cloud,
you can scale your application easily, host it without managing servers, and
take advantage of cloud services like databases and storage.
3.
Continuous Integration/Continuous Deployment (CI/CD):
Cloud platforms often support automated deployment processes, making it easier
to manage updates and maintenance.
Overview of Heroku
Heroku
is a cloud platform that allows developers to build, run, and operate
applications entirely in the cloud. It's ideal for Node.js applications because
it supports the necessary tools out-of-the-box, and deployment is relatively
simple.
Key
Heroku features:
·
Easy to use: You can deploy your app
directly from GitHub or via Git commands.
·
Free tier: Suitable for small projects
with limited resources.
·
Scalable: You can upgrade your plan to
handle more traffic.
·
Environment variable support: Securely
store configuration values that change between development and production
environments.
Lab Tasks
1. Deploying a Node.js App to Heroku
Prerequisites:
·
A Heroku account (if you don't have one,
sign up at Heroku).
·
Git installed on your system.
·
Node.js application to deploy.
Steps to Deploy a Node.js Application:
1.
Install the Heroku CLI
To interact with Heroku from your terminal, you need to install the Heroku CLI.
You can download and install it from here.
2.
Log in to Heroku
Once the Heroku CLI is installed, open your terminal or command prompt and log
in:
bashheroku login
This
will open a browser window where you can enter your Heroku account credentials.
3.
Prepare Your Node.js Application for Deployment
Ensure your Node.js application is ready to deploy:
o Make
sure you have a package.json file
with all dependencies listed.
o Ensure
your application runs locally with:
bashnpm start
o Include
a Procfile in the root
of your project. The Procfile tells Heroku
how to run your application. For a Node.js app, it should look like:
bashweb: node index.js
Replace index.js with the entry point of your application.
4.
Initialize Git Repository
If you haven't already, initialize a Git repository for your project:
bashgit init
Add
all your files to Git:
bashgit add .git commit -m "Initial commit"
5.
Create a Heroku App
Create an app on Heroku. This will set up a new remote repository on Heroku.
bashheroku create
This
command will output a URL like https://your-app-name.herokuapp.com/,
where your app will be deployed.
6.
Push Your Code to Heroku
Deploy the code by pushing it to Heroku:
bashgit push heroku master
Heroku
will automatically detect the Node.js environment and install dependencies.
7.
Access Your Application
After the push is complete, you can access your deployed application by
visiting the URL provided by Heroku (e.g., https://your-app-name.herokuapp.com/).
2. Configuring Environment Variables for Deployment
What are Environment Variables?
Environment
variables are values that are stored outside of your application’s codebase.
They are typically used to store sensitive information (like API keys, database
credentials, etc.) that shouldn't be hard-coded into the app.
For
example, instead of writing your database password directly in your code, you
would store it as an environment variable and access it from your app.
Steps to Set Up Environment Variables on Heroku:
1.
Create an Environment Variable
On Heroku, you can set environment variables using the Heroku CLI. For example,
if you want to store a database URL, you can set it like this:
bashheroku config:set DATABASE_URL=your-database-url
2.
Access Environment Variables in Your Application
In your Node.js code, you can access the environment variable using process.env. For example:
javascriptconst dbUrl = process.env.DATABASE_URL;
3.
Environment Variables in .env for Development
Locally, during development, you might use a .env
file to store your environment variables. Use the dotenv
package to load the .env
variables:
bashnpm install dotenv
Then,
in your Node.js application, load the .env
file at the top of your code:
javascriptrequire('dotenv').config();
Your .env file might look like
this:
luaDATABASE_URL=your-local-database-url
Note:
Make sure to add .env
to .gitignore
to avoid pushing sensitive data to your version control system.
4.
Verify the Environment Variable
You can check if the environment variable is correctly set on Heroku by
running:
bashheroku config
This
will list all the environment variables for your Heroku application.
Conclusion
In
this class, you've learned how to deploy a Node.js application to Heroku and
configure environment variables to ensure your app runs securely in different
environments. Deployment to the cloud opens up opportunities to scale your app,
collaborate with others, and serve your users globally.
By
following these steps, you can deploy your Node.js applications to Heroku and
start exploring more advanced topics such as scaling, monitoring, and securing
your app.
Next Steps
·
Experiment with other cloud platforms like
AWS, Google Cloud, or Azure
for deploying Node.js applications.
·
Explore Heroku add-ons
for adding databases, caching, and other services to your app.
·
Learn about Continuous
Integration/Continuous Deployment (CI/CD) pipelines to automate the
deployment process.
You
said:
WRITE 10 ASSIGNMENTS FROM
ABOVE CONCEPT WITH STEP BY STEP SOLUTIONS with proper explanation in details
ChatGPT
said:
Here
are 10 assignments based on the concept of deploying Node.js applications, with
step-by-step solutions and detailed explanations:
Assignment 1: Setting Up a Node.js Application
Objective:
Create a simple Node.js application and prepare it for deployment.
Steps:
1.
Initialize
a new Node.js project:
bashmkdir my-node-appcd my-node-appnpm init -y
2.
Create
a basic index.js
file:
javascript// index.jsconst http = require('http');const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello, World!');});server.listen(3000, () => { console.log('Server is running on port 3000');});
3.
Test
the application locally by running:
bashnode index.js
4.
Open
a browser and go to http://localhost:3000.
You should see "Hello, World!".
Assignment 2: Preparing the Node.js App for Heroku Deployment
Objective:
Set up the necessary files and configurations for deploying the app to Heroku.
Steps:
1.
Create
a Procfile in
the root directory of your project:
bashecho "web: node index.js" > Procfile
2.
Check
the contents of the Procfile:
bashcat Procfile
This
should display:
makefileweb: node index.js
3.
Ensure
that you have a package.json
file and that it contains a start script:
json{ "name": "my-node-app", "version": "1.0.0", "main": "index.js", "scripts": { "start": "node index.js" }, "dependencies": { "http": "^0.0.1" }}
4.
Commit
the changes using Git:
bashgit initgit add .git commit -m "Initial commit with app ready for Heroku"
Assignment 3: Deploying a Node.js App to Heroku
Objective:
Deploy the Node.js application to Heroku.
Steps:
1.
Install
Heroku CLI from Heroku and log in:
bashheroku login
2.
Create
a new Heroku app:
bashheroku create
3.
Push
the app to Heroku:
bashgit push heroku master
4.
Once
the push is complete, open the app in a browser:
bashheroku open
5.
You
should see "Hello, World!" displayed on the Heroku-hosted page.
Assignment 4: Configuring Environment Variables in Heroku
Objective:
Set and access environment variables in your Node.js app deployed on Heroku.
Steps:
1.
Set
an environment variable in Heroku:
bashheroku config:set DATABASE_URL=your-database-url
2.
Access
the environment variable in your Node.js application:
javascriptconst dbUrl = process.env.DATABASE_URL;console.log('Database URL:', dbUrl);
3.
Verify
that the environment variable is set by checking the Heroku config:
bashheroku config
Assignment 5: Using .env for Local Development
Objective:
Use .env files
for managing environment variables in local development.
Steps:
1.
Install
dotenv
package:
bashnpm install dotenv
2.
Create
a .env file:
luaDATABASE_URL=your-local-database-url
3.
Modify
index.js to
load the environment variable from .env:
javascriptrequire('dotenv').config();const dbUrl = process.env.DATABASE_URL;console.log('Database URL:', dbUrl);
4.
Add
.env to .gitignore:
bash.env
5.
Run
the application locally:
bashnode index.js
Assignment 6: Viewing Logs in Heroku
Objective:
View logs for your Node.js application on Heroku.
Steps:
1.
In
the terminal, run the following command to view logs:
bashheroku logs --tail
2.
Visit
your app and check the logs for incoming requests.
Assignment 7: Implementing a Simple API Endpoint
Objective:
Create a simple API endpoint that returns JSON data and deploy it to Heroku.
Steps:
1.
Modify
index.js to
include an API endpoint:
javascriptconst http = require('http');const server = http.createServer((req, res) => { if (req.url === '/api') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ message: 'Hello from API' })); } else { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello, World!'); }});server.listen(3000, () => { console.log('Server is running on port 3000');});
2.
Deploy
the updated app to Heroku:
bashgit add .git commit -m "Added API endpoint"git push heroku master
3.
Test
the /api
endpoint: Visit https://your-app-name.herokuapp.com/api
to see the JSON response.
Assignment 8: Scaling the Application in Heroku
Objective:
Scale your Node.js application to handle more traffic.
Steps:
1.
Check
the current number of dynos (instances) running:
bashheroku ps
2.
Scale
your application by increasing the number of dynos:
bashheroku ps:scale web=2
3.
Check
the number of dynos again:
bashheroku ps
4.
Open
your app and verify that it is still functioning as expected with the new
scale.
Assignment 9: Adding a Database to Your Heroku App
Objective:
Add a PostgreSQL database to your Heroku app.
Steps:
1.
Add
a PostgreSQL add-on to your app:
bashheroku addons:create heroku-postgresql:hobby-dev
2.
Access
the database URL provided by Heroku:
bashheroku config:get DATABASE_URL
3.
Update
your app to connect to PostgreSQL using an appropriate Node.js library like pg:
bashnpm install pg
4.
Modify
index.js to
connect to the PostgreSQL database:
javascriptconst { Client } = require('pg');const client = new Client({ connectionString: process.env.DATABASE_URL,});client.connect();
5.
Deploy
the updated app to Heroku:
bashgit add .git commit -m "Added PostgreSQL database connection"git push heroku master
Assignment 10: Automating Deployment with GitHub Actions
Objective:
Set up GitHub Actions for automating deployment to Heroku.
Steps:
1.
In
your GitHub repository, create a .github/workflows
directory.
2.
Create
a deploy.yml
file inside that directory with the following content:
yamlname: Deploy to Heroku on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up Node.js uses: actions/setup-node@v2 with: node-version: '14' - name: Install dependencies run: npm install - name: Heroku deploy run: | git remote add heroku https://git.heroku.com/your-app-name.git git push heroku main env: HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
3.
Add
your Heroku API key to GitHub Secrets:
o Go
to your GitHub repository.
o Under
Settings > Secrets, add a new secret with the name HEROKU_API_KEY and the value being your Heroku API
key.
4.
Push
your changes to the main
branch, and GitHub Actions will automatically deploy your app to Heroku.
Conclusion
These
assignments cover the core steps and concepts involved in deploying a Node.js
application to Heroku, managing environment variables, scaling, and automating
deployment with GitHub Actions. Completing these tasks will give you a solid
understanding of deploying and managing Node.js applications in a cloud
environment.
You
said:
WRITE 50 no of MCQs only
FROM ABOVE CONCEPT with explanation
ChatGPT
said:
Here
are 50 multiple-choice questions (MCQs) based on deploying a Node.js
application, with detailed explanations for each answer:
1. What is the purpose of the Procfile in Heroku deployment?
a) It
specifies the node version
b) It defines the commands that are run by the Heroku platform to start the app
c) It manages the database connection
d) It sets environment variables
Answer:
b) It defines the commands that are run by the Heroku platform to start the app
Explanation: The Procfile
is used to tell Heroku how to run your application. It specifies the command
Heroku should use to start your app.
2. Which command is used to deploy a Node.js app to Heroku?
a) git deploy
b) heroku deploy
c) git push heroku master
d) npm deploy
Answer:
c) git push heroku master
Explanation: The command git
push heroku master is used to deploy the app from your local
repository to Heroku's cloud platform.
3. What is the default port that a Node.js app listens to on Heroku?
a)
8080
b) 5000
c) 3000
d) 80
Answer:
c) 3000
Explanation: By default, Heroku assigns an ephemeral port to
your application. You should access it via process.env.PORT
instead of hardcoding a port like 3000.
4. What is the purpose of using dotenv in a Node.js app?
a) To
manage user authentication
b) To serve static files
c) To load environment variables from a .env
file
d) To create a new database
Answer:
c) To load environment variables from a .env
file
Explanation: The dotenv
package allows you to load environment variables from a .env file into process.env in your Node.js
app.
5. How do you access environment variables in Node.js?
a) process.env.VARIABLE_NAME
b) config.get(VARIABLE_NAME)
c) getEnv("VARIABLE_NAME")
d) env.VARIABLE_NAME
Answer:
a) process.env.VARIABLE_NAME
Explanation: In Node.js, environment variables can be accessed
using process.env.VARIABLE_NAME.
6. What is the command to view logs for your Heroku app?
a) heroku log
b) heroku logs --tail
c) heroku status
d) heroku app log
Answer:
b) heroku logs --tail
Explanation: The heroku
logs --tail command allows you to view real-time logs for your
Heroku application.
7. Which file should be included in your Node.js app for Heroku
deployment to work properly?
a) .gitignore
b) package.json
c) app.js
d) index.html
Answer:
b) package.json
Explanation: The package.json
file defines the application's dependencies, scripts, and metadata, which is
necessary for Heroku to build and run your app.
8. What is the function of the web in the Heroku Procfile?
a) It
defines the web application's runtime environment
b) It specifies the server to use for HTTP requests
c) It defines the type of dyno to be used
d) It tells Heroku to use the PostgreSQL database
Answer:
c) It defines the type of dyno to be used
Explanation: In Heroku's Procfile,
web is used
to define the type of dyno (the process type) Heroku should use to run the
application.
9. Which command is used to view environment variables in Heroku?
a) heroku env
b) heroku config --get
c) heroku config
d) heroku settings
Answer:
c) heroku config
Explanation: The heroku
config command is used to view all the environment variables
associated with your Heroku application.
10. How do you add an environment variable to your Heroku app?
a) heroku set DATABASE_URL=your_database_url
b) heroku add
DATABASE_URL=your_database_url
c) heroku config:set
DATABASE_URL=your_database_url
d) heroku env add
DATABASE_URL=your_database_url
Answer:
c) heroku config:set
DATABASE_URL=your_database_url
Explanation: The correct command to set an environment
variable in Heroku is heroku config:set
VARIABLE_NAME=value.
11. How can you automatically deploy your Node.js app from GitHub to
Heroku?
a) By
configuring a GitHub webhook
b) By using GitHub Actions
c) By using Heroku CLI
d) By setting up a custom server
Answer:
b) By using GitHub Actions
Explanation: GitHub Actions allows you to automate deployment
to Heroku whenever changes are pushed to your repository.
12. What is the primary purpose of Heroku add-ons?
a) To
add extra features to the app such as databases or caching
b) To scale the application
c) To automatically commit code changes
d) To manage the domain name of the app
Answer:
a) To add extra features to the app such as databases or caching
Explanation: Heroku add-ons are services that extend the
functionality of your app, like databases, caching, monitoring, and more.
13. What is the first step to deploy an app to Heroku?
a) Set
up a PostgreSQL database
b) Log in to Heroku using the Heroku CLI
c) Create a Procfile
d) Initialize a git repository
Answer:
b) Log in to Heroku using the Heroku CLI
Explanation: To deploy your app to Heroku, the first step is
to log in using the heroku login
command.
14. What is the purpose of the heroku
open
command?
a) It
opens the local development server
b) It opens the app in a browser
c) It opens the logs of the app
d) It opens the Heroku dashboard
Answer:
b) It opens the app in a browser
Explanation: The heroku
open command opens your app's URL in a default web browser.
15. How can you scale your app's dynos on Heroku?
a) heroku scale web=2
b) heroku dyno scale=2
c) heroku ps:scale=2
d) heroku dynos=2
Answer:
a) heroku scale web=2
Explanation: The heroku
ps:scale web=2 command is used to scale your app to run two dynos
for the web process.
16. How do you configure Heroku to use a custom port?
a) heroku config:set PORT=5000
b) heroku set PORT=5000
c) process.env.PORT
d) heroku configure
PORT=5000
Answer:
a) heroku config:set
PORT=5000
Explanation: You can configure the port to listen on by
setting the PORT
environment variable using heroku
config:set.
17. What is the default deployment method for a Node.js app to Heroku?
a) FTP
upload
b) Git push
c) Heroku CLI commands
d) ZIP file upload
Answer:
b) Git push
Explanation: The default deployment method on Heroku is to use
git push heroku master,
which pushes the app from your local repository to Heroku.
18. What is the use of the git remote add
heroku
command?
a) To
link your local app to a Heroku app
b) To push code to Heroku
c) To deploy the app from Heroku
d) To initialize a new Git repository
Answer:
a) To link your local app to a Heroku app
Explanation: git
remote add heroku is used to link your local Git repository to a
remote Heroku repository for deployment.
19. How do you specify the start command in Heroku for a Node.js app?
a)
Through heroku start
b) By setting a start
script in package.json
c) Through a custom script in the Procfile
d) Both b and c
Answer:
d) Both b and c
Explanation: You can specify the start command in both package.json under the start script and in the Procfile for Heroku to use.
20. How can you check if your app is running on Heroku?
a) Use
heroku ps
to view running dynos
b) Visit the app’s URL
c) Use heroku open
to open the app
d) All of the above
Answer:
d) All of the above
Explanation: You can check if your app is running on Heroku by
using heroku ps,
visiting its URL, or using heroku
open.
21. How do you keep track of error logs in Heroku?
a) By
using heroku logs --tail
b) By setting up a custom logging service
c) By checking logs/
folder
d) By using heroku monitor
Answer:
a) By using heroku logs --tail
Explanation: heroku
logs --tail shows the most recent log entries for your Heroku
app, which can help you track errors.
22. What should you add to the package.json file to deploy a Node.js app?
a) name, version, main
b) start
script
c) Dependencies section
d) All of the above
Answer:
d) All of the above
Explanation: The package.json
file must include basic information, the start
script, and any required dependencies for deployment.
23. What is a dyno in Heroku?
a) A
server instance running your application
b) A type of database
c) A storage space
d) A Git repository
Answer:
a) A server instance running your application
Explanation: A dyno is a lightweight container used by Heroku
to run your application.
24. How do you restart a Node.js app on Heroku?
a) heroku restart
b) heroku deploy
c) heroku ps:restart
d) heroku restart app
Answer:
a) heroku restart
Explanation: The heroku
restart command is used to restart the application on Heroku,
which can be useful after deploying updates.
25. How do you add a PostgreSQL database to your Heroku app?
a) heroku addons:add heroku-postgresql
b) heroku set DATABASE_URL=postgres://
c) heroku config:set
DATABASE_URL=postgres://
d) heroku config:add
database=postgres
Answer:
a) heroku addons:add
heroku-postgresql
Explanation: The command heroku
addons:add heroku-postgresql adds a PostgreSQL database to your
app on Heroku.
26. What is the purpose of the heroku
run
command?
a) To
deploy code to Heroku
b) To run commands in a Heroku app environment
c) To open a browser for the app
d) To update the Heroku app
Answer:
b) To run commands in a Heroku app environment
Explanation: The heroku
run command is used to run one-off commands, such as database
migrations or interactive sessions, in the Heroku environment.
27. What should you do before deploying a Node.js app to production?
a) Set
up a local development environment
b) Ensure all necessary environment variables are configured
c) Debug using console.log()
d) Ignore production-ready optimizations
Answer:
b) Ensure all necessary environment variables are configured
Explanation: Before deploying, make sure that all environment
variables, including database credentials, are set properly for production.
28. How can you configure a custom domain for your Heroku app?
a)
Through Heroku settings panel
b) Using heroku set DOMAIN
c) By editing DNS records with your domain provider
d) By using heroku domain
config
Answer:
c) By editing DNS records with your domain provider
Explanation: You configure a custom domain by updating the DNS
settings in your domain provider's control panel and linking it to your Heroku
app.
29. How do you manage user authentication in a Heroku app?
a) By
using Heroku add-ons for authentication
b) By writing custom authentication code
c) By using JWT tokens
d) All of the above
Answer:
d) All of the above
Explanation: User authentication can be handled with Heroku
add-ons, custom code, or JWT tokens.
30. What does the Heroku git push command do?
a)
Pushes code to a remote repository on GitHub
b) Deploys code from your local machine to Heroku
c) Pushes changes to a production environment
d) Creates a new Git branch
Answer:
b) Deploys code from your local machine to Heroku
Explanation: The git
push command is used to push changes from your local machine's
Git repository to Heroku for deployment.
31. What does the heroku ps:scale web=2 command do?
a)
Scales the database
b) Increases the number of web dynos to 2
c) Scales the app to production
d) Deploys the app
Answer:
b) Increases the number of web dynos to 2
Explanation: The heroku
ps:scale web=2 command is used to scale the number of web dynos
running your app on Heroku.
32. What is the best way to handle large-scale traffic on a Heroku app?
a)
Deploy the app without scaling
b) Use horizontal scaling (adding more dynos)
c) Ignore load balancing
d) Limit the number of requests
Answer:
b) Use horizontal scaling (adding more dynos)
Explanation: To handle large-scale traffic, horizontal scaling
by adding more dynos is a good solution to distribute the load.
33. How do you deploy a Node.js app with a MongoDB database on Heroku?
a) Use
MongoDB add-on
b) Use MongoDB Atlas and configure it in process.env
c) Use a custom server to manage the database
d) Both b and c
Answer:
d) Both b and c
Explanation: MongoDB Atlas is a cloud database service you can
use with Heroku. You configure it in process.env
for database connection.
34. What is the command to view all your Heroku apps?
a) heroku apps
b) heroku list
c) heroku ps
d) heroku dashboard
Answer:
a) heroku apps
Explanation: The heroku
apps command shows a list of all the apps associated with your
Heroku account.
35. Which command is used to scale dynos for the Node.js app on Heroku?
a) heroku ps:scale web=3
b) heroku scale dynos=3
c) heroku deploy --scale 3
d) heroku ps:dyno=3
Answer:
a) heroku ps:scale web=3
Explanation: The command heroku
ps:scale web=3 scales your application to use three web dynos.
36. Which service can be used to send emails from your Node.js app
deployed on Heroku?
a)
Heroku SendGrid add-on
b) Heroku Mailer
c) NodeMailer package
d) Both a and c
Answer:
d) Both a and c
Explanation: You can use either the Heroku SendGrid add-on or
the NodeMailer package to send emails from your app.
37. How do you manage database migrations in Heroku for a Node.js app?
a)
Using Heroku CLI commands
b) Using migration tools like Sequelize or Knex.js
c) By manually creating migration scripts
d) All of the above
Answer:
d) All of the above
Explanation: You can manage database migrations using
migration tools like Sequelize or Knex.js, or by running migration commands
through Heroku.
38. How do you manage background jobs in Heroku?
a) By
using Heroku's Background Worker process
b) By running background jobs on a separate server
c) By using cloud services like AWS Lambda
d) By using a cron job
Answer:
a) By using Heroku's Background Worker process
Explanation: Heroku supports background workers, which are
separate processes that run asynchronously to handle tasks like sending emails
or processing large files.
39. What is a typical Heroku app's file structure?
a) src, dist, node_modules, public, Procfile, package.json
b) app, bin, lib, node_modules, config, README.md
c) html, css, assets, static, index.html
d) routes, controllers, views, models, assets
Answer:
a) src, dist, node_modules, public, Procfile, package.json
Explanation: A typical Heroku Node.js app contains folders
like src, node_modules, and a Procfile, along with the
necessary project files such as package.json.
40. What is the first step when deploying a Node.js app on Heroku?
a)
Install Heroku CLI
b) Initialize a Node.js project with npm
init
c) Create a GitHub repository
d) Set up a PostgreSQL database
Answer:
a) Install Heroku CLI
Explanation: To deploy a Node.js app on Heroku, you first need
to install the Heroku CLI to interact with Heroku from your terminal.
41. How can you handle SSL (secure connections) for your Heroku app?
a) By
configuring the SSL certificates in the app’s settings
b) By enabling SSL on your web server
c) By using Heroku’s SSL add-on
d) All of the above
Answer:
d) All of the above
Explanation: You can manage SSL certificates using Heroku’s
SSL add-on, enable it in the web server, or configure it through Heroku's
settings.
42. What is the purpose of the Procfile in a Heroku app?
a) To
define the command used to start the app
b) To manage database connections
c) To specify the app’s version
d) To set environment variables
Answer:
a) To define the command used to start the app
Explanation: The Procfile
is used to specify the command that Heroku uses to run your application, such
as starting the web server.
43. What is Heroku's free tier?
a) 1GB
of free storage
b) 100 dyno hours per month
c) Unlimited app deployments
d) Free email support
Answer:
b) 100 dyno hours per month
Explanation: Heroku’s free tier provides 100 dyno hours per
month for running apps.
44. What is the command to view the logs of your Heroku app?
a) heroku logs
b) heroku view logs
c) heroku show logs
d) heroku tail logs
Answer:
a) heroku logs
Explanation: The command heroku
logs is used to view the logs of your app to troubleshoot issues
or monitor activity.
45. How do you connect a Node.js app to a PostgreSQL database on Heroku?
a) Use
the pg npm
package
b) Use the mysql2
npm package
c) Configure the DATABASE_URL
environment variable
d) Both a and c
Answer:
d) Both a and c
Explanation: To connect a Node.js app to a PostgreSQL database
on Heroku, you use the pg
npm package and configure the DATABASE_URL
environment variable provided by Heroku.
46. What is the Heroku command for scaling your app?
a) heroku ps:scale
b) heroku app:scale
c) heroku dyno:scale
d) heroku ps:resize
Answer:
a) heroku ps:scale
Explanation: The heroku
ps:scale command is used to scale the dynos of your app.
47. How do you manage secrets in Heroku?
a)
Using Heroku environment variables
b) Using a third-party service like AWS Secrets Manager
c) By storing them in the app's code
d) By using an encrypted file on the server
Answer:
a) Using Heroku environment variables
Explanation: You manage secrets in Heroku by setting
environment variables through the Heroku dashboard or CLI.
48. How can you update the environment variables for your Heroku app?
a)
Through the Heroku dashboard
b) Using heroku config:set
c) By editing the env.json
file
d) Both a and b
Answer:
d) Both a and b
Explanation: You can update environment variables through the
Heroku dashboard or by using the heroku
config:set command.
49. What is the purpose of the heroku
open
command?
a) It
opens the app in the browser
b) It opens the Heroku dashboard
c) It opens the Git repository
d) It opens the app's database
Answer:
a) It opens the app in the browser
Explanation: The heroku
open command opens your Heroku app in a browser.
50. How can you rollback to a previous release on Heroku?
a) heroku releases:rollback
b) heroku deploy:rollback
c) heroku git:rollback
d) heroku ps:rollback
Answer:
a) heroku releases:rollback
Explanation: The heroku
releases:rollback command rolls back to a previous release of
your app.

No comments:
Post a Comment