简体   繁体   中英

Google Cloud Functions: Use single repository for multiple functions

I've been getting annoyed trying to figure out how to keep a single repository and use it for multiple cloud functions. I didn't want to have to have multiple "projects", each with its own index.js . Also, automatically exporting every single possible function from index.js isn't efficient at all. I wanted a way to dynamically export only the function I needed for that call.

Fortunately, each time your project is called, the function it's looking for is provided in process.env.FUNCTION_NAME , so you can evaluate that and export it at run time.

function init(event, callback) {
  var fn = require('./my-event.js'); // Exports just a single function that takes the `callback`.

  fn(callback);
};
exports[process.env.FUNCTION_NAME] = init;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM