Reputation: 875
I am fairly new to Google cloud functions and by that token also new to Node.js. I have had to write a lot of Google functions and now my Index.js is quite lengthy. Is it possible for me to allocate each function to an individual file and maybe refer to them in the Index.js or perhaps group the functions in different .js files? Additionally, when deploying is there anything I need to do differently?
Upvotes: 2
Views: 613
Reputation: 8063
Try this in your index.js:
exports.hello = (request, response) => {
require('./fns/hello.js')(request, response)
}
and then in fns/hello.js
module.exports = (request, response) => {
response.json({ok: true})
}
Upvotes: 2
Reputation: 15266
Break the story into two parts. The first is to code your JavaScript such that the code lives in multiple files. This is not related to GCP. I would suggest a Google search using "node js separate files". There you will find information on breaking code into modules. Grok this and you are 75% there. The next part is to supply ALL the source files as a unit for your Google Cloud Functions ... one way to do this is to supply a ZIP file containing your source files and a package.json describing the dependencies.
Upvotes: 0