How can I share code (e.g. Mongo schema definitions) between files in an Azure function app?
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);
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.
I simply get a Exception while executing function: Functions.GetBlogPosts. mscorlib: ReferenceError: BlogPost is not defined
.
I've also tried explitely require
ing the .js file, but this seems not to be found. It could be I just got the path wrong.
Does anyone have an example or tips on how to share .js
code between azure functions?
Obtaining keys To view your keys, create new ones, or roll keys to new values, navigate to one of your HTTP-triggered functions in the Azure portal and select Function Keys. You can also manage host keys. Navigate to the function app in the Azure portal and select App keys.
Use Visual Studio Code to create a JavaScript function that responds to HTTP requests. Test the code locally, then deploy it to the serverless environment of Azure Functions. Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account. There's also a CLI-based version of this article.
A function is a primary concept in Azure Functions. You write code for a function in a language of your choice and save the code and configuration files in the same folder. The configuration is named function.json, which contains JSON configuration data. It defines the function bindings and other configuration settings.
Use the node --version command to check your version. Visual Studio Code on one of the supported platforms. The Azure Functions extension for Visual Studio Code. Azure Functions Core Tools 4.x. In this section, you use Visual Studio Code to create a local Azure Functions project in JavaScript.
Now let’s create a new function app project. This is really easy with VSCode. All you have to do is go-to the Azure Extension explorer present in the activity bar. From there access FUNCTIONS tab and click on the first Create New Project icon.
I fixed this issue by doing the following steps:
hosts.json
to watch
a shared folder. "watchDirectories": [ "Shared" ]
blogPostModel.js
file containing the following schema/model definition and exportshared\blogPostModel.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);
require
the shared file with the following path:
var blogPostModel = require('../Shared/blogPostModel.js');
I can then make a connection and interact with the model doing find
s etc in each individual function.
This solution was composed from the following SO posts:
Azure Function in Node.js and shared files
Cannot overwrite model once compiled Mongoose
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With