DEV Community

陈杨
陈杨

Posted on

HarmonyOS5 Cloud Service Technology Sharing--ArkTS Development Function

✨【A Hands-on Guide to Mastering HarmonyOS Cloud Function Debugging】✨

Hey fellow developers! Today, let's talk about how to quickly debug HarmonyOS cloud functions from the command line to supercharge your development efficiency~ 🚀

👉 Let's get to the key points first:

✅ Local debugging without waiting for packaging

✅ Supports Node.js 14.x/18.x and Java 1.8

✅ Supports HTTP trigger invocation

✅ A seamless continuous development and debugging workflow

🛠️ Getting Started:

  1. Install the AGCLI tool (AppGallery Connect Command Line Interface)
  2. Prepare a cloud function project for testing
  3. Ensure you have the correct local Node.js version (it's recommended to use nvm for version management)

🔥 The 5-Step Debugging Method:

【Step 1】Environment Configuration

Create a <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">.agclirc</font> file in your project's root directory and fill in your developer account information:

{
  "client_id": "Your_ID",
  "client_secret": "Your_Secret",
  "project_id": "Your_Project_ID"
}
Enter fullscreen mode Exit fullscreen mode

【Step 2】Write a Test Function

Here's an example 🌰 (HTTP trigger):

// index.js
exports.handler = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({ 
      message: "Hello there! Current timestamp: " + Date.now()
    })
  };
};
Enter fullscreen mode Exit fullscreen mode

【Step 3】Start Local Debugging

Open your terminal and run:

agcli function test --trigger-http
Enter fullscreen mode Exit fullscreen mode

Success looks like this:

🚀 Local service started: http://localhost:8000
Enter fullscreen mode Exit fullscreen mode

【Step 4】Send a Test Request

Open a new terminal window and try:

curl http://localhost:8000
Enter fullscreen mode Exit fullscreen mode

You should get a response like:

{"message":"Hello there! Current timestamp: 1620000000000"}
Enter fullscreen mode Exit fullscreen mode

【Step 5】Advanced Debugging Tips

▸ Real-time Log Monitoring:

agcli function logs --tail
Enter fullscreen mode Exit fullscreen mode

▸ Testing with Parameters:

curl -X POST http://localhost:8000 -d '{"name":"Developer"}'
Enter fullscreen mode Exit fullscreen mode

💡 Pitfall Guide:

  1. Getting a version error? Check if your Node.js version is 14.x or 18.x.
  2. 403 error? Double-check the key configuration in your <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">.agclirc</font> file.
  3. Local port conflict? Try using the <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">--port 8080</font> parameter.

🎯 After Successful Debugging:

Deploy directly from the command line:

agcli function deploy
Enter fullscreen mode Exit fullscreen mode

🌟 Pro Tips:

• Use the <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">--env</font> parameter to switch between test and production environments.

• For more complex scenarios, use Postman for API testing.

• Regularly clean up old test functions (you can do this from the console).

A heartfelt piece of advice: local debugging can genuinely save you 80% of the time you'd spend waiting for packaging. The sooner you adopt it, the easier your life will be! If you run into any issues during development, feel free to drop a comment and discuss~ 💬

Wishing you all smooth debugging and a bug-free journey! Next time, we'll dive into more advanced cloud function topics, so be sure to follow! 😉

(This document is compiled based on HarmonyOS ArkTS API 9+. For the latest information, please refer to the official documentation.)

Top comments (0)