Cloud Functions is an event-driven serverless compute platform from Google. You can run your code locally or in the cloud without having to provision servers. There are many ways to run nodejs application on cloud function. we are going to look for zip upload and deploy code from terminal.
On Google cloud platform select cloud function and create function.
Fill the required information
- Name - function name for your program.
- Memory allocated - as required by function processing.
- Trigger - select Http
- Source code - zip upload.
- Runtime - nodejs 10
- Function to execute - It is a name of a function exported by the module specified in the directory with source code.
- Check Advance Options if you want more control.
and finally upload a zipped nodejs application.
To deploy nodejs application with private npm package, include .npmrc
file at root level -
//npm.private.com/:_authToken="<token>"
@ng-test:registry=https://npm.private.com/
you can read private npm package auth token from ~./npmrc
or login with npm login --registry=https://registry.company-name.npme.io
and npm token list
.
In package.json
add private package name in dependencies and Google functions-framework in dev dependencies.
{
"name": "notification-service",
"version": "1.0.0",
"description": "Notification Service",
"main": "index.js",
"author": "Pritesh Kanthaliya",
"license": "UNLICENSED",
"scripts": {
"start": "npx @google-cloud/functions-framework --target=notificationService",
"deploy": "npx gcloud functions deploy notificationService --runtime nodejs10 --trigger-http",
},
"dependencies": {
"@ng-test/hello-world": "1.0.0",
},
"devDependencies": {
"@google-cloud/functions-framework": "~1.5.1",
}
}
With npm start
you can run nodejs application at local machine and test. With npm deploy
you can deploy your code on Google cloud function. If a cloud function is already present with the same name, it overrides the current one else it would create a new one.
Top comments (0)