Hello World Node.js Example
Make sure serverless
is installed. See installation guide.
Once installed the Serverless CLI can be called with serverless
or the shorthand sls
command.
$ sls
Commands
* You can run commands with "serverless" or the shortcut "sls"
* Pass "--verbose" to this command to get in-depth plugin info
* Pass "--no-color" to disable CLI colors
* Pass "--help" after any <command> for contextual help
1. Create a service
sls create --template azure-nodejs --path $(whoami)-sample-app
Using the create
command we can specify one of the available templates. For this example we use azure-nodejs with the --template
or shorthand -t
flag.
The --path
or shorthand -p
is the location to be created with the template service files.
Note
Azure plugin use a combination of:
- prefix, if provided
- service name
- region
- stage
to generate resource names. Since resource name have to be unique in Azure, adding $(whoami)
will append your username to
the service name, thus creating a unique name.
Install Provider Plugin
Change directories into the new folder created in previous step.
Run
npm install
Testing Locally
You can skip this section if you do not want to test your functions locally before deploy.
Terminal 1
sls offline
Terminal 2
sls invoke local -f hello -d '{"name": "Azure"}'
In your terminal window you should see the following response
$Serverless: URL for invocation: http://localhost:7071/api/hello?name%3DAzure
$Serverless: Invoking function hello with GET request
$Serverless: "Hello Azure"
Deploy
sls deploy
Invoke deployed function
-
Invoke deployed function with command
invoke
and--function
or shorthand-f
.sls invoke -f hello -d '{"name": "Azure"}'
-
In your terminal window you should see the response from azure
$Serverless: Logging into Azure $Serverless: Using subscription ID: A356AC8C-E310-44F4-BF85-C7F29044AF99 $Serverless: URL for invocation: http://{baseURL}.azurewebsites.net/api/hello?name%3DAzure $Serverless: Invoking function hello with GET request $Serverless: "Hello Azure"
Congrats you have deployed and ran your Hello World function!