如何准备我的Azure函数的代码

我在ES6上有一些项目。 以一个文件为例:

export default function (a, b) { return a+b; } 

我使用webpack和babel将其转换为一个带有ES2015代码的文件。 得到这样的东西:

 /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = "/"; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { module.exports = __webpack_require__(1); /***/ }, /* 1 */ /***/ function(module, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = function (a, b) { // return aCalc(a) + bCalc(b); return a + b; }; /***/ } /******/ ]); 

现在我想在Azure函数中使用我的函数。 为此,我创build文件calc.js,并用webpack输出填充它,并尝试从我的主函数中调用它:

 var calc = require('./calc'); module.exports = function (context, req) { var result = calc(2); context.done(null, result); }; 

并得到错误

执行函数时的exception:Functions.HttpTriggerJS1。 mscorlib:TypeError:calc不是在D:\ Program Files(x86)\ SiteExtensions \ Functions \ 1.0.10690上module.exports(D:\ home \ site \ wwwroot \ HttpTriggerJS1 \ index.js:11:30) \ BIN \内容\脚本\ functions.js:83:24。

现在Questin:我应该如何准备在Azure函数中调用它的代码?

基本上,您不需要将代码转换为ES5,因为在Azure函数中运行的代码是服务器端的Node.js Javascript,而不是前端Javascript。 最新的Node.js支持大部分的ES6(ES2015)语法和function。

Node.green网站基于kangax的compat-table,提供了在各种版本的Node.js中支持的ECMAScript特性的极好的概述。

所以请考虑升级Node / NPM的版本。 要做到这一点见下文。

  1. 转到浏览器中的Azure门户 。
  2. 导航到你的function应用程序,然后点击function应用程序设置
  3. 点击configuration应用设置button,然后将WEBSITE_NODE_DEFAULT_VERSION设置为7.2.0在这里输入图像描述

  4. 完成后,你在开发控制台中检查。 在这里输入图像描述

您可以在Node WebApp的URL https://<yourappname>.scm.azurewebsites.net/api/diagnostics/runtime中看到Azure支持的所有节点版本。

希望能帮助到你。

对于大多数Web应用程序,Aaron是正确的, WEBSITE_NODE_DEFAULT_VERSION将设置应用程序将运行的节点的版本。

不幸的是,azure函数不能以相同的方式运行节点,并且由于依赖于edgejs而被locking在6.5.0。

其中一个GitHub的问题有一个很好的评论 ,他们如何启用捆绑azure色的function。 在你的具体情况下,我认为你需要在bundle中包含一个你的函数需求,而不是简单地导出这个函数:

global.deps = { calc: require('./calc') };

然后,在你的函数中使用这个包:

 // require your bundle require('<nameofyourbundle>'); // dependencies are stored in the global object under 'deps' var calc = global.deps.calc; // your exported function module.exports = (context, req) => { context.done(null, calc(2)); }