A text translation tool is implemented through the Express Component of the Serverless Framework. The tool is based on Tencent Cloud TMT machine translation tool and supports mutual translation of multiple languages. Click to view the Demo deployment effect.

How do I quickly create a text translation tool using the Serverless Framework Express Component? Follow the steps below to try it out!

1. Install

Use NPM to install the latest version of Serverless Framework

$ npm install -g serverless
Copy the code

2. To create

Create and enter a new directory:

$ mkdir express-trans && cd express-trans
Copy the code

Run the following command and link to the template to quickly create an application:

$ serverless create --template-url https://github.com/tinafangkunding/serverless-translate
$ cd serverless-translate
Copy the code

Go to the SRC directory and run the following command to install the dependency

cd src && npm install
Copy the code

Configuration of 3.

Example in the template is changed to. Env, and the SecretId and SecretKey information of Tencent Cloud is obtained and configured from API Key Management.

# .env
TENCENT_SECRET_ID=123
TENCENT_SECRET_KEY=123
Copy the code

You can go to https://cloud.tencent.com/login or https://cloud.tencent.com/register tencent cloud account registration.

Deployment of 4.

Run the following command in the directory in the serverless.yml file to deploy the application

$ serverless deploy
Copy the code

After the deployment is complete, you can view the URL of the application in the command line interface. You can view the deployment result by accessing the URL. Refer to the interface documentation for supported translation language types.


Please scan QR code login from wechat. 
Wait login...
Login successful for TencentCloud. 

serverless ⚡ framework
Action: "deploy" - Stage: "dev" - App: "appDemo" - Instance: "TranslateDemo"

region: ap-beijing
apigw: 
  serviceId:   service-xxxxx
  subDomain:   service-xxxxx-1250000000.bj.apigw.tencentcs.com
  environment: release
  url:         https://service-xxxxx-1250000000.bj.apigw.tencentcs.com/release/
scf: 
  functionName: express_component_xxxx
  runtime:      Nodejs10.15
  namespace:    default

10s › TranslateDemo › Success
Copy the code

For example: serverless service-q8qqunpf-1251971143.bj.apigw.tencentcs.com/release/ input and translate it as useful simplified Chinese, language input is automatic monitoring. There is no server

5. Monitor

View application-level monitoring information in the Serverless Dashboard. The following monitoring indicators can be displayed:

  • Function invocations & errors
  • Function latency
  • API requests/errors: API requests & errors
  • API latency: API latency
  • API 5xx Error count: API 5XX errors
  • API 4XX Error count: API 4XX errors
  • Number of API errors: API errors
  • API path requests, number of requests, and average latency for different paths

6. Remove

In the directory where the serverless. Yml file resides, run the following command to remove the deployed Express service. After this component is removed, all related resources created during cloud deployment will be deleted.

$ serverless remove
Copy the code

Architecture description

In this example, the following Serverless services will be used in Tencent cloud accounts:

  • API Gateway – API Gateway will receive external requests and forward them to SCF cloud functions.
  • SCF Cloud functions – Cloud functions are used to host express.js applications.
  • CAM Access Control – This component creates default CAM roles to authorize access to associated resources.
  • COS object storage – To ensure upload speed and quality, when cloud functions compress and upload code, the code package is stored in a specially named COS bucket by default.
  • TMT Machine Translation – Call the SDK of machine translation to achieve translation capabilities, offering free credits.

References:

  • The Serverless Express Component is fully configured
  • Machine translation interface documentation

This demo is modified from 0700-Serverless-demo, thanks to the original author Yugasun.

Serverless Framework 30-day trial plan

We invite you to experience the most convenient way to develop and deploy Serverless. During the trial period, all related products and services provide free resources and professional technical support to help your business quickly and easily achieve Serverless!

For details, see: Serverless Framework Trial Plan

One More Thing

What can you do in 3 seconds? Take a sip of water, read an email, or — deploy a full Serverless application?

Copy the link to PC browser to access: serverless.cloud.tencent.com/deploy/expr…

Fast deployment in 3 seconds, experience the fastest Serverless HTTP development in history immediately!

Portal:

  • GitHub: github.com/serverless
  • Website: serverless.com

Welcome to Serverless Chinese, you can experience more about Serverless application development in the best practices!


Recommended reading: Serverless Architecture: From Principle, Design to Project Practice