How To Share Code In Javascript Azure Functions?
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,
Solution 1:
I fixed this issue by doing the following steps:
- Add a line to the root
hosts.json
towatch
a shared folder."watchDirectories": [ "Shared" ]
- In the shared folder, added a
blogPostModel.js
file containing the following schema/model definition and export
shared\blogPostModel.js
varmongoose= require('mongoose');
varSchema= mongoose.Schema;
varblogPostSchema=newSchema({
id: 'number',
title: 'string',
date: 'date',
content: 'string'
});
module.exports = mongoose.model('BlogPost', blogPostSchema);
- In my function
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:
Post a Comment for "How To Share Code In Javascript Azure Functions?"