Skip to content

Instantly share code, notes, and snippets.

@joshlarsen
Created March 19, 2019 18:38
Show Gist options
  • Save joshlarsen/e74bec10a99641c0192ae37ae9e5d8f5 to your computer and use it in GitHub Desktop.
Save joshlarsen/e74bec10a99641c0192ae37ae9e5d8f5 to your computer and use it in GitHub Desktop.
Google Cloud Functions Node.js 8 Simple Emulator
/**
* Inspiration taken from here https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/258#issuecomment-413786762
* Alternative: https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/258#issuecomment-407996126
*
* Original: https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/258#issuecomment-437080813
*/
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(cors());
app.options('*', cors());
const triggers = require('./index');
app.use(bodyParser.json());
const PORT = process.env.PORT || 8080;
for (let name in triggers) {
// create a trigger for every export
console.info(`Registered function http://localhost:${PORT}/${name}`);
app.post(`/${name}`, (request, response) => {
response.setTimeout(540000);
console.log(`hit -> ${name}`);
triggers[name](request, response);
});
}
// Start the server
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment