1

I have a server.js file inside a connections folder that I'm trying to use and have all my azure functions reference so that it doesn't instantiate a connection after every API call. I'm having issues exporting the connection and importing it correctly in my azure functions. Is this not possible. If it is, what am I doing wrong? Appreciate the help!

server.js

const redis = require('redis');
var mongoose = require('mongoose');
const redis = require('redis');

const client = redis.createClient(6380, process.env.REDISCACHEHOSTNAME + '.redis.cache.windows.net', {
  auth_pass: process.env.REDISCACHEKEY,
  tls: { servername: process.env.REDISCACHEHOSTNAME + '.redis.cache.windows.net' }
});

module.exports = { client };

top of azure function file importing connection

const redis = require('redis');

var redisConnection = require("../connections/server")


console.log("THE CLIENT IS " + redisConnection.client.connected);


Error: client is not defined

0

1 Answer 1

1

Yes, it is possible. I have tested this on Azure portal. I created two http trigger functions named HttpTrigger1 and HttpTrigger2.

HttpTrigger1 function

enter image description here

HttpTrigger2 function

enter image description here

It seems that your code is correct, please check your function project directory structure, mine is

enter image description here

You can test it locally, here are my code for your reference:

The server.js file

const redis = require('redis');

const client = redis.createClient(6380, 'test.redis.cache.windows.net', 
        {auth_pass: 'XXXX=', tls: {servername: 'test.redis.cache.windows.net'}});

module.exports = { client };

The function code

const redis = require('redis');

var redisConnection = require("../connections/server")

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    context.log("THE CLIENT IS " + redisConnection.client.connected);
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: "THE CLIENT IS " + redisConnection.client.connected
    };

};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.