我想知道是否有人使用带有 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/