Translation | "JavaScript Everywhere" Chapter 10 Deploying Our API ( _ )
Write at the top
Hello everyone, I am Mao Xiaoyou, a front-end development engineer. An English technical book is being translated.
In order to improve everyone's reading experience, the structure and content of sentences have been slightly adjusted. If you find any flaws in this article, or if you have any comments or suggestions, you can leave a message in the comment area, or add my WeChat: code_maomao, welcome to communicate and learn from each other.
(σ゚∀゚)σ…:*☆Oh, good
Chapter 10 Deploying Our API
Imagine that if a user wants API
to create, read, update or delete a note every time through ours , they must only use a personal computer. Currently, this is how we API
work because it only runs on our personal computers. We can Web
solve this problem by deploying the application to the server.
In this chapter, we will be divided into two steps:
-
First, we will create a
API
remote database that we can access. -
Second, we
API
deploy the code to the server and connect it to the database.
After completing these steps, we can use it to access from any networked computer (including the Web
desktop and mobile interface to be developed ) API
.
Host our database
In the first step, we will use a managed database solution. For our Mongo
database, we will use MongoDB Atlas
.
This is Mongo
a fully managed cloud product supported by a supporting organization. In addition, they provide a free experience that can implement our initial deployment well. Let us step through MongoDB Atlas
the steps to deploy to .
First, browse mongodb.com/cloud/atlas
and create an account. After creating an account, you will be prompted to create a database. On this screen, you can manage the settings of the sandbox database, but I recommend using the default values for now.
these are:
-
Amazon's
AWS
as the database host, but also provides Google's cloud platform and Microsoft'sAzure
. -
Nearest area with "free tier" option.
-
Cluster layer, the default value is "
M0
sandbox (sharingRAM
,512MB
storage)" -
Other settings, we can leave them as default settings.
-
The cluster name, we can leave it as the default name.
Click "Create Cluster" from here. At this point, it Mongo
will take a few minutes to set up the database (Figure 10-1
).
Figure 10-1
. MongoDB Atlas
Database creation screen
Next, you will see the Clusters
page where you can manage your single database cluster (picture 10-2
).
Graph 10-2MongoDB Atlas
cluster
On the "Cluster" screen, click "Connect" and you will be prompted to set up connection security. The first step is to IP
whitelist your address. Since our application will have a dynamic IP
address, you need to use 0.0.0.0/0
it to open it to any IP
address. After all IP
addresses are whitelisted, you will need to set up a secure username and password to access the data (picture 10-3
).
Figure 10-3.MongoDB Atlas IP
whitelist and user account management
After IP
whitelisting and creating a user account, you will choose the connection method for the database. In this case, it will be the "application" connection (pictured 10-4
).
Figure 10-4
. In MongoDB Atlas
selecting the connection type
From here, you can copy the connection string. env
file (picture 10-5
) that we will use in production .
Figure 10-5
. MongoDB Atlas
Database connection string
Mongo password
MongoDB Atlas
Perform hexadecimal encoding for special characters in the password. This means that if you use (and should use!) any non-letter or numeric value, you will need to use the hexadecimal value of that code when adding the password to the connection string. The website ascii.cl
provides corresponding hexadecimal codes for all special characters. For example, if your password is Pizz
@ 2
!. you will need to encode @ and! character. You can use% and then add the hexadecimal value. The password will be generated Pizz
% 402
%21.
With our MongoDB Atlas
managed database up and running, we now provide a managed data store for our application. In the next step, we will host our application code and connect it to our database.
Deploy our application
Our next deployment setup is to deploy our application code. For the purposes of this book, we will use a cloud application platform Heroku
. I chose Heroku
because of its superior user experience and free free package, but other cloud platforms (such as Amazon Web Services
, Google Cloud Platform
, Digital Ocean
or Microsoft Azure
) are to Node.js
provide an alternative to the application hosting environment.
Before we start, you need to visit Heroku
the website and create an account. After creating an account, you need to install Heroku
command line tools for your operating system .
For in macOS
, you can use the Homebrew
following installation Heroku
command line tools:
$ brew tap heroku/brew && brew install heroku
For Windows
users, please visit the Heroku
command line tool guide and download the corresponding installer.
Project settings
After installing the Heroku
command line tool, we can Heroku
set up the project on the website. By clicking "New" → "Create New Application" (Figure 10-6
).
Figure 10-6
. Heroku
New application dialog
Here, you will be prompted to specify a unique name for the application, and then click the "Create Application" button (pictured 10-7
). Go on, you will see the qualified name whenever you use YOUR_APP_NAME.
Figure 10-7
. Provide a unique application name
Now we can add environment variables. Similar to how we use our .ENV
local files, we can Heroku
manage our production environment variables in the website interface. To do this, click "Settings" and then the "Show Configuration Variables" button. In this screen, add the following configuration variables (picture 10-8
):
NODE_ENV production
JWT_SECRET A_UNIQUE_PASSPHRASE
DB_HOST YOUR_MONGO_ATLAS_URL
Figure 10-8
. Heroku
Environment variable configuration
We are ready to deploy our code.
Deployment method
Now, we are ready to deploy the code to Heroku
the server. For this, we can use simple Git
commands in the terminal application . We will Heroku
set up as a remote endpoint, then add and submit changes, and finally push the code to Heroku
. To do this, run the following command in the terminal application:
$ heroku git:remote -a <YOUR_APP_NAME>
$ git add .
$ git commit -am "application ready for production"
$ git push heroku master
As you Heroku
build and deploy the file, you should see output in the terminal. When finished, we Heroku
will use package.json
the run script in the file to run our application on its server.
test
After successfully deploying our application, we will be able to make GraphQL API
requests to our remote server . By default, it is GraphQL Playground UI
disabled in production, but we can use the curl
test application in the terminal application . To run the curl
request, enter the following in the terminal application:
$ curl \
-X POST \
-H "Content-Type: application/json" \
--data '{ "query": "{ notes { id } }" }' \
https://YOUR_APP_NAME.herokuapp.com/api
If the test is successful, we will receive a notes
response with an empty array, because our production database does not yet contain any data:
{
"data":{
"notes":[]}}
In this way, we have deployed the application!
in conclusion
In this chapter, we use cloud services to deploy the database and application code. MongoDB Atlas
And Heroku
other services enable developers to launch small applications and extend them to anywhere from hobby projects to high-traffic businesses. By deploying ours API
, we have successfully developed the back-end service of the application stack. In the following chapters, we will focus on the application UI
.
If there is any inadequate understanding, please correct me. If you think it's okay, please like to collect or share it, hoping to help more people.