azure - 具有 azure 函数和 webpack 的无服务器

标签 azure webpack azure-functions serverless-framework serverless-architecture

我想知道是否有人使用带有 azure 函数的无服务器框架,以及如何处理跨函数和捆绑的共享代码?

我正在将 hapi.js 应用程序转换为 serverless + serverless-azure-functions我尝试在部署之前捆绑我的代码,以便我可以使用各种 require 来实现可重用模块。

我找到了serverless-webpack它创建的 bundle 可能适用于 AWS Lambda,但由于缺少 function.json 文件(例如 list-function.json),因此在 azure 上存在问题,因此这些函数在 azure-portal 中根本不可见,我也无法调用它们。

还发现article关于这个问题,但它展示了如何使用仅支持Windows平台的azure-functions-cli来处理这个问题。

最好,JH

最佳答案

https://medium.com/a-man-with-no-server/deploying-a-serverless-application-using-webpack-and-babel-to-support-es2015-to-aws-2f61cff8bafb 获取提示,我用serverless-webpack修改了一个serverless azurefunctions启动测试项目,似乎可以满足您的要求。

我在serverless azure function项目的根目录下建了一个src文件夹,作为develop源代码文件夹。有2个测试文件:
handler.js

'use strict';
let tool = require("./tool");
/* eslint-disable no-param-reassign */

module.exports.hello = function (context) {
  context.log('JavaScript HTTP trigger function processed a request.');

  context.res = {
    // status: 200, /* Defaults to 200 */
    body: tool.hello(),
  };

  context.done();
};

tool.js

module.exports={
    hello:()=>{
        return "hello world";
    }
}

根目录中的webpack.config.js:

var nodeExternals = require('webpack-node-externals')

module.exports = {
   entry: './src/handler.js',
   target: 'node',
   externals: [nodeExternals()],
   output: {
      libraryTarget: 'commonjs',
      path: __dirname,
      filename: 'handler.js', // this should match the first part of function handler in serverless.yml
   },
   module: {
      loaders: [
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            include: __dirname,
            loaders: ["babel-loader"]
         }
      ]
   }
};

使用哪个配置文件,捆绑出来的文件将位于根目录的service/handler.js中。

所以我也修改了serverless.yml,现在它部分看起来像:

package:
  include:
    - service/handler.js
  exclude:
    - handler.js

functions:
  hello:
    handler: service/handler.hello
    events:
      - http: true
        x-azure-settings:
          authLevel : anonymous
      - http: true
        x-azure-settings:
          direction: out
          name: res

custom:
  webpackIncludeModules:
    packagePath: './package.json'

完成这些修改后,使用serverless deploy将文件捆绑在src文件夹中,然后打包并部署到azure函数。

希望有帮助。

关于azure - 具有 azure 函数和 webpack 的无服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46154758/

相关文章:

azure - 保护 Azure 函数的安全

azure - MSAL idToken 没有 upn、个人资料或电子邮件数据?需要在 idToken 中进行 Passport-azure-ad 授权

typescript - source-map-loader 在哪里适合使用 Webpack 进行 TypeScript 编译?

javascript - Webpack 插件 : How to dynamically add a loader to a module?

azure - 在 java 中执行 Azure 函数应用程序时获取 NullPointerException : Stack: java. lang.reflect.InitationTargetException

java - 如何使用 Android Volley 将参数发送到 Azure Functions?

c# - 检测 IIS 何时在 Azure 中回收

postgresql - Azure PostgreSQL 外部数据包装器 postgres_fdw

Azure 容器实例运行 FTP 端口未打开

reactjs - 将 React 构建输出合并到单个 js 文件中