Migrate Express to function calculation

Keywords: npm Mobile Docker Linux

First of all, it introduces some important concepts in this paper:

Function calculation: function calculation is an event driven service. Through function calculation, users do not need to manage the operation of the server, just write code and upload. Function computing prepares computing resources and runs user code in an elastic and scalable way, while users only need to pay according to the resources consumed by the actual code running. Function calculation more Reference resources. Fun: Fun is a tool used to support the application deployment of Serverless, which can help you manage function calculation, API gateway, log service and other resources conveniently. It uses a resource configuration file (template.yml) to help you develop, build, and deploy. More documents for fun Reference resources.

Note: the technique described in this article requires that the Fun version be greater than or equal to 3.6.3.

background information

Express It is a simple and flexible Web application development framework based on Node.js platform. It provides a series of powerful features to help you create various Web and mobile device applications.

Step 1: prepare the environment

The method described in this article does not need to install Docker, just install Fun. The simplest way is to download executable binary files directly.

  1. Install Fun to the machine. For details, see Installation document.
  2. Run fun --version to check if the installation is successful.

Step 2: migrate the application

  1. Execute the following command to create an Express project. For details, see Express application generator , or you can create a simple Hello world example , skip this step if there is an Express project.

    npx express-generator
    
  2. Execute the following command to enter the sample project you just created or your project.

    	cd <project-name>
    
  3. Execute the following command depending on the installation.

    	npm install
    
  4. Run the project by executing the following command.

  • MacOS and Linux platform operation project:
    DEBUG=myapp:* npm start
    
  • windows platform running project:
    set DEBUG=myapp:* & npm start
    
  1. Execute the command fun deploy -y to deploy the project to the function calculation. Fun will automatically enter the deployment process.

    	$fun deploy -y
    	current folder is not a fun project.
    	Generating /Users/txd123/Desktop/Express/bootstrap...
    	Generating template.yml...
    	Generate Fun project successfully!
    	========= Fun will use 'fun deploy' to deploy your application to Function Compute! =========
    	using region: cn-qingdao
    	using accountId: ***********3743
    	using accessKeyId: ***********Ptgk
    	using timeout: 60
    
    	Collecting your services information, in order to caculate devlopment changes...
    
    	Resources Changes(Beta version! Only FC resources changes will be displayed):
    	...  ...  ...  ...
    					trigger httpTrigger deploy success
    			function Express deploy success
    	service Express deploy success
    
    	Detect 'DomainName:Auto' of custom domain 'Domain'
    	Request a new temporary domain ...
    	The assigned temporary domain is 15795585-XXX.test.functioncompute.com,expired at 2020-04-12 10:46:25, limited by 1000 per day.
    	Waiting for custom domain Domain to be deployed...
    	custom domain Domain deploy success
    

    After the deployment, you can see from the log of successful deployment that the function calculation generates a temporary domain name for you, through which you can directly access the newly deployed application.

    **Note: * * the temporary domain name is only used for demonstration and development, which is time-effective. If it is used for production, please bind the domain name that has been filed in Alibaba cloud. For details, see Bind custom domain name.

summary

This article describes how to deploy Express application to function calculation. Compared with the traditional deployment method, you can deploy the traditional Express application one click to the far end directly for production. While skipping the steps of purchasing machines, it also has the features of elastic expansion, pay as you go and no operation and maintenance.

"Alibaba cloud native Pay attention to microservices, Serverless, containers, Service Mesh and other technology fields, focus on cloud native popular technology trends, cloud native large-scale landing practice, and become the technology circle that most understands cloud native developers. "

Posted by mariom on Tue, 07 Apr 2020 23:06:38 -0700