I have a folder named functions, inside there is index.js.
On index.js I have the main function that accept all http :
exports.contentServer = functions.https.onRequest((request, response) => {
Since I have many more functions I need to organize my files, and I want to add another file, say payments.js, so that index.js can call functions inside payments.js, or even onCreate callbacks will be fired from payment.js.
If I just create another js file in functions, it won't work.
What has to be done to be able to have a single Cloud Function in another new js file ?
You can use a standard nodejs require to load code in other files.
index.js
const otherFunctions = require('./other.js');
exports.contentServer = functions.https.onRequest((request, response) => {
otherFunctions.other()
}
other.js
exports.other = function() { ... }
I suggest reading the documentation for more information.
Related
I'm creating a React application that also has a folder of helper functions to use later. The helper functions are written in plain JS. I created a test file for the helper functions but when I run tests I only see the App.test.js results.
I can't tell what I'm missing. Below is the helper function test file:
import { IsValidBinary } from "./validityFunctions";
test("01101 should be binary", () => {
const binString = "01101";
const isValid = IsValidBinary(binString);
expect(isValid).toBe(true);
});
In this official Firebase example it's described how to structure cloud functions to minimize cold start times.
The main idea seems to be the following in the index.ts file:
export const httpFn =
functions.https.onRequest(async (request, response) => {
await (await import('./fn/httpFn')).default(request, response)
})
In other words using await( await import()). This example is however in typescript. How would this look in node.js? I've found some examples. Eg. this one.
In there the index.js file has:
exports.helloWorld = require('./hello-world').helloWorld
And the individual function files look like:
const functions = require('firebase-functions')
exports.helloWorld = functions.https.onRequest((request, response) => {
const output = {
di: 'is cool',
}
response.send(output)
})
Is this the optimal method to minimize cold start time in node.js? Given I can't use "await import". Will this structure only load the necessary dependencies for each individual function? Or will it still load all dependencies across all required (imported) files?
That blog post you're referring to is not an "official sample". It's a description of my own personal thoughts and opinions.
What you're showing here is not equivalent to what's shown in the blog post. If you want a pure JavaScript implementation of TypeScript's async import, you will have to write code to mimic that. require() is not asynchronous and doesn't achieve the same thing.
I suggest reading other questions on Stack Overflow on this topic:
Node.js - asynchronous module loading
How to async require in nodejs
I am trying to break my nodejs code up into as small of files as possible to keep things modular. I have an app.js that is needing to use another method that I have moved to utils.js. The utils file however has a method that has a requirement for another library.
Should my utils.js file be "requiring" dependencies or should all those go in my app.js?
// app.js
var utilities = require('./utilities');
..
return utilities.anotherMethod();
// utils.js
module.exports = {
producer: function () {
// Handle mapping of fields depending on the source system
return 'map fields'
},
anotherMethod: function () {
// I require another lib. Do I do that here or app.js?
var kafka = require('kafka-node');
}
};
Update: Regarding the close request for an opinion based answer is essentially telling me that this can be done either way, which is what I was trying to clarify.
I have generated my app with express --view=pug myapp which created me a folder-tree with the files I need to start over.. I wrote some code which I would like to outsource from the main app.js in maybe a function-file or something like that, to keep the app.js cleaner.
where would I put my custom functions? how would I then require the function-file in nodeJS ?
You can arrange your files as you wish. Wherever you keep your functions, just add the functions you want to use in any other files to module.exports object in that file. Then in your app.js (or any other file where you want to use these functions), import the file using require and you should have access to all the exported properties and functions from the file you import.
For example:
I can put my functions in ./lib/core-lib.js:
function test(){
// do something
}
module.exports = {
test: test
};
And then in my app.js
const lib = require('./lib/core-lib');
lib.test();
I recently tried to organize my code by exporting functions into different files, so my main file stays clean. To do so I imported the other js file in my main file with
const request = require('./request.js');
In "request.js" I used export, so I can call the function in my main file. Is it is possible to call a function, that is defined in the main file, from the "request.js" file? Unfortunately I can't just return the information back to the main file, because I am using callbacks.
Yes it's possible.
You just need to export function from the main file and then import your main file inside request.js:
// main.js:
module.exports.someFunction = function() {
// Some code
};
const request = require('./request.js');
request.someRequestFunction();
// request.js
var main = require('./main.js');
module.exports = {
someRequestFunction: function() {
main.someFunction(); // Call function from main module
}
};