Web Quick Start
Preparations
- Have a Tencent Cloud account
- Be sure to Create a CloudBase environment and obtain the Environment ID.
- Install Node.js
- Install Cloudbase CLI
npm install -g @cloudbase/cli
If npm install -g @cloudbase/cli
fails, you may need to modify npm permissions, or run the command as an administrator:
sudo npm install -g @cloudbase/cli
Step 1: Create Initial Project
- MacOS or Linux
- Windows
Use the command line to create a directory named my-cloudbase-app and two files under it.
mkdir my-cloudbase-app && cd my-cloudbase-app && touch index.html && touch cloudbaserc.json
There are two files in this directory: index.html
and `cloudbaserc.json
├── cloudbaserc.json
└── index.html
- Create the
my-cloudbase-app
folder - In this folder, create two blank files
index.html
and `cloudbaserc.json
Below is the content of index.html
. We attempt to log in to CloudBase, and if successful, a pop-up window will appear.
<html>
<head>
<meta charset="utf-8" />
<!-- Visit the npm registry to check the latest version https://www.npmjs.com/package/@cloudbase/js-sdk -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/${version}/cloudbase.full.js"></script>
<script>
const app = cloudbase.init({
env: "Your Environment ID", // Enter your environment ID here
});
app
.auth()
.anonymousAuthProvider()
.signIn()
.then(() => {
alert("Logged in to CloudBase successfully!");
});
</script>
</head>
<body>
Hello Cloudbase!
</body>
</html>
Below is the content of cloudbaserc.json
:
{
"envId": "Your Environment ID"
}
Step 2: Adding Security Domains
Log in to the CloudBase Console, and in the Security Configuration page, add the domain to the Web Security Domains.
Here we add localhost:5000
to the security domains, allowing pages under this domain to use the SDK to access CloudBase services.
Step 3: Enable Anonymous Login
Refer to: Enabling Anonymous Login Authorization
Step 4: Enable Local Development Environment
Run in the project root directory:
npx serve
A local static server will start, and you can access http://localhost:5000
If the SDK successfully logs in using an anonymous identity, you should see a pop-up window.
After successful login, you can access and utilize various CloudBase resources. For details, please refer to the Web SDK Documentation
Step 5 (Optional): Deploy Static Pages Using CloudBase
- Enable Static Website Hosting
- Run the following command in the project root directory to upload the website files:
cloudbase hosting deploy index.html
Before running cloudbase hosting deploy
, log in to the CLI tool:
cloudbase login
- Use envId-instanceId.tcloudbaseapp.com to access your website, where
envId
andinstanceId
are the identifiers of your CloudBase environment.
For details, refer to the Static Website Hosting documentation.