Skygear offers pre-configured containerization templates so that developers can skip writing a Dockerfile when deploying. One of these templates is Node.js, which we will be using to write a microservice in this section.
Before you start this section, please ensure you have skycli
installed and configured properly. If you haven't, follow this to set it up.
Create a directory called nodeserver
and go inside:
$ mkdir nodeserver && cd $_
Enter the skycli
app creation command and give the new app a name. It's better to prefix you app name with your name or preferred alias like your GitHub ID, since Skygear cluster is shared among everyone who has an account and it's likely someone has already taken the app name nodeserver
.
Upon app creation success, app information such as its API endpoint and key will be listed. They are essential for API calling which we will be performing at later stages. You can either jot them down now or find them at Skygear's Developer Portal.
$ skycli app create? What is your app name? <your_name>-nodeserverApp name: <your_name>-nodeserver.Creating app...Your API endpoint: https://<your_name>-nodeserver.skygearapp.com/.Your Client API Key: <API Key>.Your Master API Key: <Master Key>.Created app successfully!
You will be asked a few questions on scaffolding the newly created app in your current directory.
Answer the first three questions with a Y:
? Do you want to scaffold your app now? Or you can do it later by `skycli app scaffold` command.Scaffold now? (Y/n) Y
? You're about to initialze a Skygear app in this directory: <your-local-directory>Confirm? (Y/n) Y
? All files in <your-local-directory> would be DELETED. Confirm? (Y/n) Y
Pick <your_name>-nodeserver
:
? Select an app to be associated with the directory: (Use arrow keys)❯ <your_name>-nodeserver
Select template Node.js Express
:
? Select template: (Use arrow keys)Empty❯ Node.js ExpressNode.js MongoDB Blog AppNode.js React/Express Fortune App
A success message will then be displayed:
Success! Initialized "Node.js Express" template for <your_name>-nodeserver in <your-local-directory>.
Your directory should now look like this:
.├── README.md├── frontend│ ├── index.js│ └── package.json└── skygear.yaml
A skygear.yaml
is always needed in order to deploy. It's a configuration file carrying information about the deployment. Let's have a look at the one created by our Node.js Express template:
./skygear.yamlapp: <your_name>-nodeserver # your Skygear app nameapi_version: v2.1deployments:- name: frontend # an arbitrary name of the servicetype: http-service # type of the servicecontext: frontend # root directory of your source codetemplate: nodejs:12 # pre-configured templatepath: / # the path for the serviceport: 8080 # the listening port of the service
The Skygear app name to be linked with this deployment is specified at line 1. A microservice namedfrontend
is declared at line 4, with its properties lying underneath. Line 6 tells Skygear where the source code lies, while line 8 defines the endpoint to reach the service.
Skygear runs on an actual Kubernetes cluster which is container-based, still you might already notice there exists no Dockerfile in the directory frontend. This is because we have used a pre-configured template to build our Node.js program, as displayed at line 7. With a valid value given to the configuration filed template
, you don't need to provide a Dockerfile anymore.
Ensure you are at the root of nodeserver
where skygear.yaml
lies, run:
$ skycli app deploy
Wait for the deployment to complete.
Simply curl it:
$ curl https://<your_name>-nodeserver.skygearapp.com/Hello, world!
To exclude deploying specific files, create a .skyignore
file. It works the same as .dockerignore
.
To deploy service on runtimes other than NodeJS, you can supply a custom Dockerfile
instead. Learn about it here.