{"id":1313,"date":"2023-03-18T11:20:26","date_gmt":"2023-03-18T03:20:26","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1313"},"modified":"2023-04-29T09:13:27","modified_gmt":"2023-04-29T01:13:27","slug":"develop-cloud-functions-using-unicloud","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/develop-cloud-functions-using-unicloud\/","title":{"rendered":"uniCloud\u5f00\u53d1\u4e91\u51fd\u6570"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>\u4e91\u51fd\u6570\u662f\u8fd0\u884c\u5728\u4e91\u7aef\u7684<code>JavaScript<\/code>\u4ee3\u7801\uff0c\u548c\u666e\u901a\u7684<code>Node.js<\/code>\u5f00\u53d1\u4e00\u6837\uff0c\u719f\u6089<code>Node.js<\/code>\u7684\u5f00\u53d1\u8005\u53ef\u4ee5\u76f4\u63a5\u4e0a\u624b\u3002<\/p>\n<p>\u5982\u4e0b\u662f\u5c06\u4f20\u5165\u7684\u4e24\u4e2a\u53c2\u6570\u6c42\u548c\u5e76\u8fd4\u56de\u5ba2\u6237\u7aef\u7684\u4e91\u51fd\u6570\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-javascript\">&#039;use strict&#039;;\nexports.main = async (event, context) =&gt; {\n    \/\/event\u4e3a\u5ba2\u6237\u7aef\u4e0a\u4f20\u7684\u53c2\u6570\n    return {\n    sum:event.a + event.b\n  }\n}<\/code><\/pre>\n<p>\u4e91\u51fd\u6570\u7684\u4f20\u5165\u53c2\u6570\u6709\u4e24\u4e2a\uff0c\u4e00\u4e2a\u662f<code>event<\/code>\u5bf9\u8c61\uff0c\u4e00\u4e2a\u662f<code>context<\/code>\u5bf9\u8c61\u3002<code>event<\/code>\u6307\u7684\u662f\u89e6\u53d1\u4e91\u51fd\u6570\u7684\u4e8b\u4ef6\uff0c\u5f53\u5ba2\u6237\u7aef\u8c03\u7528\u4e91\u51fd\u6570\u65f6\uff0c<code>event<\/code>\u5c31\u662f\u5ba2\u6237\u7aef\u8c03\u7528\u4e91\u51fd\u6570\u65f6\u4f20\u5165\u7684\u53c2\u6570\u3002<code>context<\/code>\u5bf9\u8c61\u5305\u542b\u4e86\u6b64\u5904\u8c03\u7528\u7684\u8c03\u7528\u4fe1\u606f\u548c\u8fd0\u884c\u72b6\u6001\uff0c\u53ef\u4ee5\u7528\u5b83\u6765\u4e86\u89e3\u670d\u52a1\u8fd0\u884c\u7684\u60c5\u51b5\u3002<code>uniCloud<\/code>\u4f1a\u81ea\u52a8\u5c06\u5ba2\u6237\u7aef\u7684\u64cd\u4f5c\u7cfb\u7edf\uff08<code>os<\/code>\uff09\u3001\u8fd0\u884c\u5e73\u53f0\uff08<code>platform<\/code>\uff09\u3001\u5e94\u7528\u4fe1\u606f\uff08<code>appid<\/code>\uff09\u7b49\u6ce8\u5165<code>context<\/code>\u4e2d\uff0c\u5f00\u53d1\u8005\u53ef\u901a\u8fc7<code>context<\/code>\u83b7\u53d6\u6bcf\u6b21\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\uff0c\u5982\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">&#039;use strict&#039;;\nexports.main = async (event, context) =&gt; {\n  \/\/event\u4e3a\u5ba2\u6237\u7aef\u4e0a\u4f20\u7684\u53c2\u6570\n  ...\n  \/\/context\u4e2d\u53ef\u83b7\u53d6\u5ba2\u6237\u7aef\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\n  let os = context.OS \/\/\u5ba2\u6237\u7aef\u64cd\u4f5c\u7cfb\u7edf\uff0c\u8fd4\u56de\u503c\uff1aandroid\u3001ios    \u7b49\n  let platform = context.PLATFORM \/\/\u8fd0\u884c\u5e73\u53f0\uff0c\u8fd4\u56de\u503c\u4e3a mp-weixin\u3001app-plus\u7b49\n  let appid = context.APPID \/\/ manifest.json\u4e2d\u914d\u7f6e\u7684appid\n  let clientIP = context.CLIENTIP \/\/ \u5ba2\u6237\u7aefip\u4fe1\u606f\n  let clientUA = context.CLIENTUA \/\/ \u5ba2\u6237\u7aefuser-agent\n    ... \/\/\u5176\u5b83\u4e1a\u52a1\u4ee3\u7801\n}<\/code><\/pre>\n<p><strong>\u5173\u4e8eCLIENTIP\u3001CLIENTUA<\/strong><\/p>\n<p>\u901a\u8fc7\u7ba1\u7406\u7aef\u8c03\u7528\u4e91\u51fd\u6570\uff08\u4e91\u51fd\u6570\u4e0a\u4f20\u5e76\u8fd0\u884c\u3001\u817e\u8baf\u4e91\u5f00\u53d1\u8c03\u8bd5\u671f\u95f4\uff09\uff0c\u4f7f\u7528\u817e\u8baf\u4e91\u65f6\u60f3\u4e34\u65f6\u8c03\u8bd5UA\u3001IP\u7b49\u53ef\u4ee5\u901a\u8fc7\u81ea\u884c\u521d\u59cb\u5316<code>uniCloud<\/code>\u7684\u65b9\u5f0f\uff08<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/uniapp.dcloud.io\/uniCloud\/init\" title=\"\u540c\u65f6\u8fde\u591a\u670d\u52a1\u7a7a\u95f4\">\u540c\u65f6\u8fde\u591a\u670d\u52a1\u7a7a\u95f4<\/a>\uff09\u4f20\u5165<code>debugFunction: false<\/code>\u6765\u5b9e\u73b0\u5ba2\u6237\u7aef\u76f4\u8fde\u8c03\u7528\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u6b64\u65f6\u63a7\u5236\u53f0\u5c06\u4e0d\u4f1a\u6253\u5370\u4e91\u51fd\u6570\u65e5\u5fd7\u3002<\/p>\n<p>\u4e91\u51fd\u6570\u4e2d\u5982\u679c\u8981\u4f7f\u7528\u5176\u4ed6\u670d\u52a1\uff08\u6bd4\u5982MySQL\u6570\u636e\u5e93\u3001Redis\u7b49\uff09\uff0c\u53ef\u4ee5\u6309\u7167<code>Node.js<\/code>\u7684\u5199\u6cd5\u5373\u53ef\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u670d\u52a1\u5546\u4e3a\u963f\u91cc\u4e91\u65f6\uff0c\u6682\u4e0d\u53ef\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bfb\u53d6\u6587\u4ef6\uff08\u6bd4\u5982<code>fs.readFileSync(&#039;.\/info.txt&#039;)<\/code>\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<code>fs.readFileSync(path.resolve(__dirname,&#039;.\/info.txt&#039;))<\/code><\/p>\n<h2>\u8bbf\u95ee\u6570\u636e\u5e93<\/h2>\n<p>\u4e91\u51fd\u6570\u4e2d\u652f\u6301\u8bbf\u95ee\u672c\u670d\u52a1\u7a7a\u95f4\u4e0b\u7684\u6570\u636e\u5e93\uff0c\u8c03\u7528\u65b9\u5f0f\u8be6\u89c1\u89c4\u8303<\/p>\n<h2>\u8bbf\u95eeHTTP\u670d\u52a1<\/h2>\n<p><code>uniCloud<\/code>\u63d0\u4f9b\u4e86<code>uniCloud.httpclient<\/code>\u4f9b\u5f00\u53d1\u8005\u4f7f\u7528\u3002\u65e0\u9700\u989d\u5916\u4f9d\u8d56\uff0c\u5c31\u53ef\u4ee5\u8bf7\u6c42\u4efb\u4f55<code>HTTP<\/code>\u548c<code>HTTPS<\/code>\u534f\u8bae\u7684 Web \u670d\u52a1\u3002<code>uniCloud.httpclient<\/code>\u8fd4\u56de\u7684\u662f\u4e00\u4e2a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/node-modules\/urllib\" title=\"urllib\u5b9e\u4f8b\">urllib\u5b9e\u4f8b<\/a>\u3002<\/p>\n<pre><code class=\"language-javascript\">uniCloud.httpclient.request(URL, requestOptions)<\/code><\/pre>\n<p><code>requestOptions<\/code>\u53c2\u6570\u8bf4\u660e\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/uniapp.dcloud.io\/uniCloud\/cf-functions\">https:\/\/uniapp.dcloud.io\/uniCloud\/cf-functions<\/a><\/p>\n<p>\u793a\u4f8b\u4ee3\u7801<\/p>\n<pre><code class=\"language-javascript\">const res = await uniCloud.httpclient.request(apiUrl, {\n    method: &#039;POST&#039;,\n    data: {\n      test: &#039;testValue&#039;\n    },\n    dataType: &#039;json&#039;\n  })\nconsole.log(res)<\/code><\/pre>\n<h2>\u4f7f\u7528npm<\/h2>\n<p>\u5728\u4e91\u51fd\u6570\u4e2d\u6211\u4eec\u53ef\u4ee5\u5f15\u5165\u7b2c\u4e09\u65b9\u4f9d\u8d56\u6765\u5e2e\u52a9\u6211\u4eec\u66f4\u5feb\u7684\u5f00\u53d1\u3002\u4e91\u51fd\u6570\u7684\u8fd0\u884c\u73af\u5883\u662f<code>Node.js<\/code>\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>npm<\/code>\u5b89\u88c5\u7b2c\u4e09\u65b9\u4f9d\u8d56\u3002<\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u9274\u4e8e\u963f\u91cc\u4e91\u7684\u9650\u5236\uff0c\u76ee\u524d\u4ec5\u652f\u6301\u5168\u91cf\u4e0a\u4f20\u4e91\u51fd\u6570\uff08\u6574\u4e2a<code>node_modules<\/code>\u6587\u4ef6\u5939\u5168\u90e8\u4e0a\u4f20\uff09\uff0c\u56e0\u6b64\u5c3d\u91cf\u7cbe\u7b80\u4f9d\u8d56\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u6bcf\u6b21\u4e0a\u4f20\u65f6\u95f4\u5f88\u6162\uff0c\u5f71\u54cd\u5f00\u53d1\u4f53\u9a8c<\/p>\n<p>\u6ce8\u610f\uff1a\u76ee\u524d\u6bcf\u4e2a\u4e91\u51fd\u6570\u4e0a\u4f20\u5305\u5927\u5c0f\u9650\u5236\u4e3a<code>10M<\/code><\/p>\n<\/blockquote>\n<h2>\u5ba2\u6237\u7aef\u8c03\u7528\u4e91\u51fd\u6570<\/h2>\n<p>\u524d\u7aef\u4ee3\u7801\uff08H5\u524d\u7aef\u3001App\u3001\u5c0f\u7a0b\u5e8f\uff09\uff0c\u4e0d\u518d\u6267\u884c<code>uni.request<\/code>\u8054\u7f51\uff0c\u800c\u662f\u901a\u8fc7<code>uniCloud.callFunction<\/code>\u8c03\u7528\u4e91\u51fd\u6570\uff0c<code>callFunction<\/code>\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<h3>\u8bf7\u6c42\u53c2\u6570<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5b57\u6bb5<\/th>\n<th style=\"text-align: left;\">\u7c7b\u578b<\/th>\n<th style=\"text-align: left;\">\u5fc5\u586b<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><code>name<\/code><\/td>\n<td style=\"text-align: left;\"><code>String<\/code><\/td>\n<td style=\"text-align: left;\">\u662f<\/td>\n<td style=\"text-align: left;\">\u4e91\u51fd\u6570\u540d\u79f0<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>data<\/code><\/td>\n<td style=\"text-align: left;\"><code>Object<\/code><\/td>\n<td style=\"text-align: left;\">\u5426<\/td>\n<td style=\"text-align: left;\">\u5ba2\u6237\u7aef\u9700\u8981\u4f20\u9012\u7684\u53c2\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u54cd\u5e94\u53c2\u6570<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5b57\u6bb5<\/th>\n<th style=\"text-align: left;\">\u7c7b\u578b<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><code>result<\/code><\/td>\n<td style=\"text-align: left;\"><code>Object<\/code><\/td>\n<td style=\"text-align: left;\">\u4e91\u51fd\u6570\u6267\u884c\u7ed3\u679c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>requestId<\/code><\/td>\n<td style=\"text-align: left;\"><code>String<\/code><\/td>\n<td style=\"text-align: left;\">\u8bf7\u6c42\u5e8f\u5217\u53f7\uff0c\u7528\u4e8e\u9519\u8bef\u6392\u67e5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<pre><code class=\"language-javascript\">\/\/ promise\u65b9\u5f0f\nuniCloud.callFunction({\n    name: &#039;test&#039;,\n    data: { a: 1 }\n  })\n  .then(res =&gt; {});\n\n\/\/ callback\u65b9\u5f0f\nuniCloud.callFunction({\n    name: &#039;test&#039;,\n    data: { a: 1 },\n    success(){},\n    fail(){},\n    complete(){}\n});<\/code><\/pre>\n<h2>\u4e91\u51fd\u6570\u4e2d\u8c03\u7528\u4e91\u51fd\u6570<\/h2>\n<p>\u7528\u6cd5\u540c\u5ba2\u6237\u7aef\u8c03\u7528\u4e91\u51fd\u6570\uff0c\u4e0d\u652f\u6301<code>callback<\/code>\u5f62\u5f0f<\/p>\n<h3>\u8bf7\u6c42\u53c2\u6570<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5b57\u6bb5<\/th>\n<th style=\"text-align: left;\">\u7c7b\u578b<\/th>\n<th style=\"text-align: left;\">\u5fc5\u586b<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><code>name<\/code><\/td>\n<td style=\"text-align: left;\"><code>String<\/code><\/td>\n<td style=\"text-align: left;\">\u662f<\/td>\n<td style=\"text-align: left;\">\u4e91\u51fd\u6570\u540d\u79f0<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>data<\/code><\/td>\n<td style=\"text-align: left;\"><code>Object<\/code><\/td>\n<td style=\"text-align: left;\">\u5426<\/td>\n<td style=\"text-align: left;\">\u4e91\u51fd\u6570\u53c2\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u54cd\u5e94\u53c2\u6570<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5b57\u6bb5<\/th>\n<th style=\"text-align: left;\">\u7c7b\u578b<\/th>\n<th style=\"text-align: left;\">\u5fc5\u586b<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><code>errCode<\/code><\/td>\n<td style=\"text-align: left;\"><code>String<\/code><\/td>\n<td style=\"text-align: left;\">\u5426<\/td>\n<td style=\"text-align: left;\">\u72b6\u6001\u7801\uff0c\u64cd\u4f5c\u6210\u529f\u5219\u4e0d\u8fd4\u56de<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>errMsg<\/code><\/td>\n<td style=\"text-align: left;\"><code>String<\/code><\/td>\n<td style=\"text-align: left;\">\u5426<\/td>\n<td style=\"text-align: left;\">\u9519\u8bef\u63cf\u8ff0<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>result<\/code><\/td>\n<td style=\"text-align: left;\"><code>Object<\/code><\/td>\n<td style=\"text-align: left;\">\u5426<\/td>\n<td style=\"text-align: left;\">\u4e91\u51fd\u6570\u6267\u884c\u7ed3\u679c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>requestId<\/code><\/td>\n<td style=\"text-align: left;\"><code>String<\/code><\/td>\n<td style=\"text-align: left;\">\u5426<\/td>\n<td style=\"text-align: left;\">\u8bf7\u6c42\u5e8f\u5217\u53f7\uff0c\u7528\u4e8e\u9519\u8bef\u6392\u67e5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<pre><code class=\"language-javascript\">let callFunctionResult = await uniCloud.callFunction({\n    name: &quot;test&quot;,\n    data: { a: 1 }\n})<\/code><\/pre>\n<h2>\u6ce8\u610f\u4e8b\u9879<\/h2>\n<h3>\u4e91\u51fd\u6570\u7684\u542f\u52a8\u6a21\u5f0f\uff08\u51b7\u542f\u52a8\u3001\u70ed\u542f\u52a8\uff09<\/h3>\n<p>\u57fa\u4e8e\u4e91\u51fd\u6570\u6309\u9700\u6267\u884c\u7684\u7279\u70b9\uff0c\u51fd\u6570\u5728\u4e0d\u88ab\u89e6\u53d1\u7684\u65f6\u5019\uff0c\u8ba1\u7b97\u8d44\u6e90\u662f\u4e0d\u88ab\u6fc0\u6d3b\u7684\u3002\u5f53\u4e00\u4e2a\u4e91\u51fd\u6570\u521d\u6b21\u88ab\u89e6\u53d1\u65f6\uff0c\u5176\u5b8c\u6574\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5b9e\u4f8b\u5316\u8ba1\u7b97\u5b9e\u4f8b<\/li>\n<li>\u52a0\u8f7d\u51fd\u6570\u4ee3\u7801<\/li>\n<li>\u542f\u52a8 node<\/li>\n<li>\u6267\u884c\u4ee3\u7801<\/li>\n<\/ol>\n<p>\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6267\u884c\u8fd9\u4e9b\u5b8c\u6574\u6b65\u9aa4\u7684\u8fc7\u7a0b\u4e00\u822c\u79f0\u4f5c\u51b7\u542f\u52a8\uff0c\u51b7\u542f\u52a8\u7684\u8017\u65f6\u957f\u4e8e\u70ed\u542f\u52a8\uff0c\u4e00\u822c\u5728\u4e00\u79d2\u51fa\u5934\u3002<\/p>\n<p>\u800c\u5982\u679c\u51fd\u6570\u5b9e\u4f8b\u548c\u6267\u884c\u8fdb\u7a0b\u90fd\u88ab\u590d\u7528\u7684\u60c5\u51b5\u4e0b\u4e00\u822c\u88ab\u5b9a\u4e49\u4e3a\u70ed\u542f\u52a8\uff0c\u70ed\u542f\u52a8\u6ca1\u6709\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u4e91\u51fd\u6570\u5b9e\u4f8b\u957f\u65f6\u95f4\u6ca1\u6709\u88ab\u518d\u6b21\u8c03\u7528\uff0c\u5219\u8be5\u8ba1\u7b97\u5b9e\u4f8b\u4f1a\u88ab\u56de\u6536\uff1b\u540e\u7eed\u518d\u6b21\u8c03\u7528\u8be5\u4e91\u51fd\u6570\u65f6\uff0c\u5c31\u4f1a\u518d\u6b21\u89e6\u53d1\u4e91\u51fd\u6570\u7684\u51b7\u542f\u52a8\u3002<\/p>\n<p>\u4e0d\u540c\u4e91\u5382\u5546\u7684\u51fd\u6570\u5b9e\u4f8b\u56de\u6536\u65f6\u95f4\uff0c\u4ee5\u53ca\u4f18\u5316\u51b7\u542f\u52a8\u7684\u5efa\u8bae\uff0c<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/uniapp.dcloud.io\/uniCloud\/faq?id=\u4e91\u51fd\u6570\u8bbf\u95ee\u65f6\u5feb\u65f6\u6162\u600e\u4e48\u56de\u4e8b\uff1f\" title=\"\u53c2\u8003\">\u53c2\u8003<\/a><\/p>\n<p>\u56e0\u4e3a\u5b58\u5728\u51b7\u70ed\u542f\u52a8\u7684\u5dee\u5f02\uff0c\u4e91\u51fd\u6570\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u5c31\u53ef\u80fd\u51fa\u73b0\u6bcf\u6b21\u4e0d\u4e00\u6837\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u4ee5\u5982\u4e0b\u4ee3\u7801\u4e3a\u4f8b\uff0c<code>count<\/code>\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u5f53\u591a\u6b21\u8c03\u7528\u8be5\u4e91\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u53d8\u91cf\u7d2f\u52a0\u7684\u60c5\u51b5\uff08\u5b9e\u4f8b\u672a\u590d\u7528\u65f6\uff0c\u6bcf\u6b21\u8fd4\u56de0\uff0c\u82e5\u5b9e\u4f8b\u88ab\u590d\u7528\uff0c\u5219\u53ef\u80fd\u8fd4\u56de1\u30012\u30013\u7b49\u5404\u79cd\u610f\u5916\u60c5\u51b5\uff09<\/p>\n<pre><code class=\"language-javascript\">let count = 0;\nmodule.exports = async (event) =&gt; {\n  return count++\n  \/\/\u6b64\u793a\u4f8b\u4e3a\u9519\u8bef\u793a\u4f8b\n  \/\/\u4e91\u51fd\u6570\u5b9e\u4f8b\u672a\u590d\u7528\u65f6\uff0c\u6bcf\u6b21\u8fd4\u56de0\n  \/\/\u82e5\u5b9e\u4f8b\u88ab\u590d\u7528\uff0c\u5219\u53ef\u80fd\u8fd4\u56de1\u30012\u30013\u7b49\u5404\u79cd\u610f\u5916\u60c5\u51b5\n}<\/code><\/pre>\n<h3>\u4e34\u65f6\u5b58\u50a8\u7a7a\u95f4<\/h3>\n<p>\u4e91\u51fd\u6570\u662f\u8fd0\u884c\u5728\u4e91\u7aef\u7684\u4ee3\u7801\uff0c\u8fd0\u884c\u73af\u5883\u7531\u4e91\u670d\u52a1\u5668\u5f39\u6027\u8c03\u914d\uff0c\u8fd9\u662f\u548c\u4f20\u7edf<code>Node.js<\/code>\u5e94\u7528\u5f88\u5927\u7684\u533a\u522b\u3002<\/p>\n<p>\u6362\u8a00\u4e4b\uff0c\u4e91\u51fd\u6570\u6bcf\u6b21\u6267\u884c\u7684\u5bbf\u4e3b\u73af\u5883\uff08\u53ef\u7b80\u5355\u7406\u89e3\u4e3a\u865a\u62df\u673a\u6216\u670d\u52a1\u5668\u786c\u4ef6\uff09\u53ef\u80fd\u76f8\u540c\uff0c\u4e5f\u53ef\u80fd\u4e0d\u540c\uff0c\u56e0\u6b64\u4f20\u7edf<code>Node.js<\/code>\u5f00\u53d1\u4e2d\u5c06\u90e8\u5206\u4fe1\u606f\u5b58\u50a8\u672c\u5730\u786c\u76d8\u6216\u5185\u5b58\u7684\u65b9\u6848\u5c31\u4e0d\u518d\u9002\u5408\uff0c\u5efa\u8bae\u901a\u8fc7\u4e91\u6570\u636e\u5e93\u6216\u4e91\u5b58\u50a8\u7684\u65b9\u6848\u66ff\u4ee3\u3002<\/p>\n<h3>\u4e91\u51fd\u6570\u4e2d\u7684\u5f02\u6b65\u884c\u4e3a<\/h3>\n<p>\u4e66\u5199\u4e91\u51fd\u6570\u65f6\u5e94\u6ce8\u610f<code>async<\/code>\u3001<code>await<\/code>\u7684\u4f7f\u7528\uff0c<code>Node.js<\/code>\u6709\u5185\u7f6e\u6a21\u5757<code>util<\/code>\u53ef\u4ee5\u5c06\u7b26\u5408<code>error-first<\/code>\u5f62\u5f0f<code>callback<\/code>\u7684\u51fd\u6570\u8f6c\u6362\u4e3a<code>promise<\/code>\u5f62\u5f0f\uff0c<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/nodejs.org\/api\/util.html#util_util_promisify_original\" title=\"\u8be6\u60c5\u53c2\u8003\">\u8be6\u60c5\u53c2\u8003<\/a>\uff0c\u6bd4\u5982\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">const {\n    promisify\n} = require(&#039;util&#039;)\n\nlet testCallback = {\n    value: &#039;testCallbackValue&#039;,\n    echo: function(num, callback) {\n        setTimeout(() =&gt; {\n      \/\/ \u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3aerror\uff0c\u7b2c\u4e8c\u4e2a\u4e3a\u8fd4\u56de\u503c\n            callback(null, `${this.value}:${num}`)\n        }, 2000)\n    }\n}\n\nexports.main = async function() {\n  \/\/ num=2\uff0c\u4e0d\u4f20\u5165callback\u53c2\u6570\uff0ccallback\u4f1a\u81ea\u52a8\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u5904\u7406\n    let val = await promisify(testCallback.echo).call(testCallback, 2)\n    console.log(val)\n    return val\n}<\/code><\/pre>\n<p>\u5982\u679c\u60f3\u5728\u4e91\u51fd\u6570\u5185\u4f7f\u7528\u56de\u8c03\u5f62\u5f0f\u53ef\u4ee5\u8ba9\u4e91\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a<code>promise<\/code>\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">exports.main = async function() {\n    return new Promise((resolve, reject) =&gt; {\n        setTimeout(() =&gt; {\n            resolve(&#039;some return value&#039;)\n        }, 1000)\n    })\n}<\/code><\/pre>\n<h3>\u5176\u5b83<\/h3>\n<ul>\n<li>\u4e91\u51fd\u6570\u4e2d\u4f7f\u7528\u7684\u65f6\u533a\u662f<code>UTC+0<\/code>\uff0c\u800c\u4e0d\u662f<code>UTC+8<\/code>\uff0c\u5728\u4e91\u51fd\u6570\u4e2d\u4f7f\u7528\u65f6\u95f4\u65f6\u9700\u7279\u522b\u6ce8\u610f<\/li>\n<li>\u4f7f\u7528\u963f\u91cc\u4e91\u4f5c\u4e3a\u670d\u52a1\u5546\u65f6\uff0c\u6682\u65f6\u65e0\u6cd5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bfb\u53d6\u6587\u4ef6\uff0c\u5982\uff1a<code>fs.readFileSync(&#039;.\/info&#039;)<\/code>\uff0c\u53ef\u4ee5\u66ff\u6362\u4e3a<code>fs.readFileSync(path.resolve(__dirname,&#039;.\/info&#039;))<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u4e91\u51fd\u6570\u662f\u8fd0\u884c\u5728\u4e91\u7aef\u7684JavaScript\u4ee3\u7801\uff0c\u548c\u666e\u901a\u7684Node.js\u5f00\u53d1\u4e00\u6837\uff0c\u719f\u6089Node.js\u7684\u5f00\u53d1\u8005 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-1313","post","type-post","status-publish","format-standard","hentry","category-uni-app"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1313"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1313\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}