如何在Azure中导出node.js函数?
我具有以下结构,其中有2个Azure函数在Azure函数应用中分别运行:
- main_function_app
- functions
- Full_Stack
- configAPI.json
- configAPITest.json
- function.json
- index.js
- Web
- configAPI.json
- configAPITest.json
- function.json
- index.js
- host.json
- local.settings.json
- package-lock.json
- package.json
- proxies.json
- utils.js
我在index.js.js
中具有以下内容,来自full_stack
:
module.exports = async function (context, req) {
let element = req.body;
const purchase_time = Number(element.purchase_time_);
const type = "Full Stack";
if (element.startswith('full')){
async function getStatusPayload(purchase_time, offset);
}
async function getStatusPayload(purchase_time, type){
if (purchase_time>0){
context.log("Purchase time successful in " + type);
}
}
context.res = {
// status: 200, /* Defaults to 200 */
status: 200
};
}
我在index.js
来自Web
:
module.exports = async function (context, req) {
let element = req.body;
const purchase_time = Number(element.purchase_time_);
const type = "Web";
if (element.startswith('web')){
async function getStatusPayload(purchase_time, type);
}
async function getStatusPayload(purchase_time, type){
if (purchase_time>0){
context.log("Purchase time successful in " + type);
}
}
context.res = {
// status: 200, /* Defaults to 200 */
status: 200
};
}
两个功能几乎相同,我想重构getStatusPayload
。我已经在utils.js
文件中创建了草稿,但是我无法在完整的堆栈或Web索引中对其进行测试,因为我不知道如何从Azure导入它。另外,我不确定一旦我能够将其导入完整堆栈和Web,代码是否会工作。
// utils.js
exports ={
getStatusPayload: async function (purchase_time, type){
if (purchase_time>0){
context.log("Purchase time successful in " + type);
}
}
}
}
module.exports = exports;
我还围绕context.executionContext.FunctionDirectory
抛出当前目录,但不确定这是否对解决方案有帮助。
I have the following structure with 2 azure functions running separately in an azure function app:
- main_function_app
- functions
- Full_Stack
- configAPI.json
- configAPITest.json
- function.json
- index.js
- Web
- configAPI.json
- configAPITest.json
- function.json
- index.js
- host.json
- local.settings.json
- package-lock.json
- package.json
- proxies.json
- utils.js
I have the following content in the index.js
from Full_Stack
:
module.exports = async function (context, req) {
let element = req.body;
const purchase_time = Number(element.purchase_time_);
const type = "Full Stack";
if (element.startswith('full')){
async function getStatusPayload(purchase_time, offset);
}
async function getStatusPayload(purchase_time, type){
if (purchase_time>0){
context.log("Purchase time successful in " + type);
}
}
context.res = {
// status: 200, /* Defaults to 200 */
status: 200
};
}
I have the following content in the index.js
from Web
:
module.exports = async function (context, req) {
let element = req.body;
const purchase_time = Number(element.purchase_time_);
const type = "Web";
if (element.startswith('web')){
async function getStatusPayload(purchase_time, type);
}
async function getStatusPayload(purchase_time, type){
if (purchase_time>0){
context.log("Purchase time successful in " + type);
}
}
context.res = {
// status: 200, /* Defaults to 200 */
status: 200
};
}
Both functions are almost the same and I want to refactor the getStatusPayload
. I have created a draft in the utils.js
file but I cannot test it in the either the full stack or web index.js because I don't know how to import it from azure. Also, I am not totally sure if code will even work once I am able to import it to full stack and web.
// utils.js
exports ={
getStatusPayload: async function (purchase_time, type){
if (purchase_time>0){
context.log("Purchase time successful in " + type);
}
}
}
}
module.exports = exports;
I also came around context.executionContext.functionDirectory
which throws the current directory, but not sure if this might be helpful in in the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的问题上:要求是我们可以用来导入其他模块的功能:
并导出:
To your question: require is a function we can use to import other modules:
and to export: