<tfoot id='HHNCc'></tfoot>
      1. <i id='HHNCc'><tr id='HHNCc'><dt id='HHNCc'><q id='HHNCc'><span id='HHNCc'><b id='HHNCc'><form id='HHNCc'><ins id='HHNCc'></ins><ul id='HHNCc'></ul><sub id='HHNCc'></sub></form><legend id='HHNCc'></legend><bdo id='HHNCc'><pre id='HHNCc'><center id='HHNCc'></center></pre></bdo></b><th id='HHNCc'></th></span></q></dt></tr></i><div id='HHNCc'><tfoot id='HHNCc'></tfoot><dl id='HHNCc'><fieldset id='HHNCc'></fieldset></dl></div>
      2. <small id='HHNCc'></small><noframes id='HHNCc'>

        <legend id='HHNCc'><style id='HHNCc'><dir id='HHNCc'><q id='HHNCc'></q></dir></style></legend>
          <bdo id='HHNCc'></bdo><ul id='HHNCc'></ul>

      3. 如何在 JavaScript Azure Functions 中共享代码?

        How to share code in JavaScript Azure Functions?(如何在 JavaScript Azure Functions 中共享代码?)
              <legend id='qUsWh'><style id='qUsWh'><dir id='qUsWh'><q id='qUsWh'></q></dir></style></legend>

            1. <small id='qUsWh'></small><noframes id='qUsWh'>

                <tbody id='qUsWh'></tbody>
                <bdo id='qUsWh'></bdo><ul id='qUsWh'></ul>
                <tfoot id='qUsWh'></tfoot>
                <i id='qUsWh'><tr id='qUsWh'><dt id='qUsWh'><q id='qUsWh'><span id='qUsWh'><b id='qUsWh'><form id='qUsWh'><ins id='qUsWh'></ins><ul id='qUsWh'></ul><sub id='qUsWh'></sub></form><legend id='qUsWh'></legend><bdo id='qUsWh'><pre id='qUsWh'><center id='qUsWh'></center></pre></bdo></b><th id='qUsWh'></th></span></q></dt></tr></i><div id='qUsWh'><tfoot id='qUsWh'></tfoot><dl id='qUsWh'><fieldset id='qUsWh'></fieldset></dl></div>
                  本文介绍了如何在 JavaScript Azure Functions 中共享代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在 Azure 函数应用中的文件之间共享代码(例如 Mongo 架构定义)?

                  How can I share code (e.g. Mongo schema definitions) between files in an Azure function app?

                  我需要这样做,因为我的函数需要访问共享的 mongo 架构和模型,例如这个基本示例:

                  I need to do this, as my functions require access to a shared mongo schema and models, such as this basic example:

                  var blogPostSchema = new mongoose.Schema({
                    id: 'number',
                    title: 'string',
                    date: 'date',
                    content: 'string'
                  });
                  
                  var BlogPost = mongoose.model('BlogPost', blogPostSchema);
                  

                  我尝试在 host.json 中添加 "watchDirectories": [ "Shared" ] 行,并在该文件夹中添加了 index.html.js 包含上述变量定义,但这似乎不适用于其他函数.

                  I've tried to add a "watchDirectories": [ "Shared" ] line to my host.json and in that folder added an index.js containing the above variable definition but this doesn't seem to be available to the other functions.

                  我只是在执行函数时得到一个异常:Functions.GetBlogPosts.mscorlib:ReferenceError:未定义博客帖子.

                  I simply get a Exception while executing function: Functions.GetBlogPosts. mscorlib: ReferenceError: BlogPost is not defined.

                  我也尝试过明确地require .js 文件,但这似乎没有找到.可能是我走错了路.

                  I've also tried explitely requireing the .js file, but this seems not to be found. It could be I just got the path wrong.

                  有人有关于如何在 azure 函数之间共享 .js 代码的示例或提示吗?

                  Does anyone have an example or tips on how to share .js code between azure functions?

                  推荐答案

                  我通过以下步骤解决了这个问题:

                  I fixed this issue by doing the following steps:

                  1. 在根 hosts.json 中添加一行以 watch 共享文件夹.watchDirectories":[共享"]
                  2. 在共享文件夹中,添加了一个 blogPostModel.js 文件,其中包含以下架构/模型定义和导出
                  1. Add a line to the root hosts.json to watch a shared folder. "watchDirectories": [ "Shared" ]
                  2. In the shared folder, added a blogPostModel.js file containing the following schema/model definition and export

                  sharedlogPostModel.js

                  var mongoose = require('mongoose');
                  var Schema = mongoose.Schema;
                  
                  var blogPostSchema = new Schema({
                      id: 'number',
                      title: 'string',
                      date: 'date',
                      content: 'string'
                  });
                  module.exports = mongoose.model('BlogPost', blogPostSchema);
                  

                  1. 在我的函数 require 中,共享文件的路径如下:var blogPostModel = require('../Shared/blogPostModel.js');
                  1. In my function require the shared file with the following path: var blogPostModel = require('../Shared/blogPostModel.js');

                  然后我可以建立连接并与模型交互,在每个单独的函数中执行 find 等操作.

                  I can then make a connection and interact with the model doing finds etc in each individual function.

                  此解决方案由以下 SO 帖子组成:

                  This solution was composed from the following SO posts:

                  Node.js 中的 Azure 函数和共享文件

                  Mongoose 编译后无法覆盖模型

                  这篇关于如何在 JavaScript Azure Functions 中共享代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                  How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                  Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                  SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                  Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                  Azure Functions - Import Custom Node Module(Azure Functions - 导入自定义节点模块)
                    <tbody id='EmdbV'></tbody>

                  <small id='EmdbV'></small><noframes id='EmdbV'>

                  <legend id='EmdbV'><style id='EmdbV'><dir id='EmdbV'><q id='EmdbV'></q></dir></style></legend>
                    <bdo id='EmdbV'></bdo><ul id='EmdbV'></ul>

                    <i id='EmdbV'><tr id='EmdbV'><dt id='EmdbV'><q id='EmdbV'><span id='EmdbV'><b id='EmdbV'><form id='EmdbV'><ins id='EmdbV'></ins><ul id='EmdbV'></ul><sub id='EmdbV'></sub></form><legend id='EmdbV'></legend><bdo id='EmdbV'><pre id='EmdbV'><center id='EmdbV'></center></pre></bdo></b><th id='EmdbV'></th></span></q></dt></tr></i><div id='EmdbV'><tfoot id='EmdbV'></tfoot><dl id='EmdbV'><fieldset id='EmdbV'></fieldset></dl></div>
                        1. <tfoot id='EmdbV'></tfoot>