44
loading...
This website collects cookies to deliver better user experience
brew update
brew install node
npm init
to get started.ProTip: you can run npm init -y
, to keep all the default values.
mkdir simple-node-api
cd simple-node-api
npm init -y
--save-dev
flag on the npm install
command. You’ll only use it locally to run the application.npm install --save-dev nodemon
npm install --save @splitsoftware/splitio express
package.json
file. These lines define how to start the app for production and start the app for development using nodemon."scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js", // <-- add this line
"dev": "nodemon index.js" // <-- add this line
}
index.js
file that defines one primary GET endpoint that will be used for this demo.const express = require('express');
const port = process.env.port || 8000;
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => { console.log('Our App Is Up And Running!'); });
module.exports = app
npm run dev
. If everything is set up correctly, you should see the following output:http://localhost:8000
with your browser, and you should see the following:curl -i http://localhost:8000
npm install mocha supertest --save-dev
const request = require("supertest");
const app = require("../index");
describe("GET /", () => {
it("responds with Hello World!", (done) => {
request(app).get("/").expect("Hello World!", done);
})
});
...
"scripts": {
"test": "mocha ./test/* --exit", // <-- modify this line
"dev": "nodemon index.js"
},
...
echo node_modules > .gitignore
git init
git remote add origin [email protected]:<your github handle>/simple-node-api.git
git add .
git commit -m "initial commit"
git push origin master
simple-node-api
project in GitHub and click Actions. Click New workflow. There’s a built-in template for Node.js projects, so that makes it easy to get started. Click Set up this workflow under Node.js.node.js.yml
. If you look carefully, you’ll notice that this file is being created in your GitHub repo. Here’s what the YAML file looks like:name: Node.js CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
ProTip: make sure to run git pull origin main
on your local machine as you’ve made changes to the repo on GitHub. This gets it back in sync.
index.js
file and replace the word World
with There
:app.get('/', (req, res) => {
res.send('Hello There!'); // edit this line
});
npm test
again. Revert the changes and push up to GitHub again, and you should see that the build is passing once again.simple-node-api
repo. Click Secrets. Click New repository secret. In the Name field, type: HEROKU_API_KEY
. In the Value field, paste the Heroku API Key that you copied. Click Add secret.heroku apps:create <unique name>
git push heroku main
NOTE: All apps in Heroku must have unique names, and simple-node-api
is already taken. In my case, I used: micah-simple-node-api
. Use a name that works for you.
curl https://<unique name>.herokuapp.com
build
. We’re now going to add a job called deploy
( NOTE : whitespace matters with YAML. The deploy section should be at the same indentation as the build
section).name: Node.js CI/CD ## <-- it’s not just CI anymore
## <-- don’t change anything in this section
jobs:
build:
## don’t change anything in this section
deploy: ## <-- this is the new stuff
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/[email protected]
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "micah-simple-node-api"
heroku_email: "[email protected]"
needs
indicates that this job is dependent on the build
job. If the build
job fails, the deploy
job will not execute.heroku_api_key
makes use of this. This is the mechanism GitHub supplies to ensure that we’re not publishing secrets directly in our repos.node.js.yml
and push it up to your GitHub repo. Click on the Actions tab again. You can click on the new workflow job that gets launched to see its progress. You should notice that the deploy
job is run and completes successfully.index.js
file so that we can see our Split treatments at work and how you might use them in production to trigger different implementations for your customers. Replace the code in index.js
with the following:const express = require('express');
const port = process.env.PORT || 8000;
const app = express();
var SplitFactory = require('@splitsoftware/splitio').SplitFactory;
var factory = SplitFactory({
core: {
authorizationKey: process.env.SPLIT_API_KEY
}
});
var splitClient = factory.client();
var getTreatment = function() {
return splitClient.getTreatment('ANONYMOUS_USER', 'hello-treatment');
}
splitClient.on(splitClient.Event.SDK_READY, function () {
console.log('split.io sdk is ready');
console.log('treatment is: ' + getTreatment());
});
app.get('/', (req, res) => {
let treatment = getTreatment();
if (treatment == 'on') {
res.send('Hello, Your Treatment is ON!');
} else if (treatment == 'off') {
res.send('Hello, Your Treatment is OFF!');
} else {
res.send('Hello, This Is The Default!');
}
});
app.listen(port, () => { console.log('Our App Is Up And Running!'); });
module.exports = app
npm test
and you’ll see what I mean. Update your test.js
file to the following:const request = require("supertest");
const app = require("../index");
describe("GET /", () => {
it("responds with Hello, This Is The Default!", (done) => {
request(app).get("/").expect("Hello, This Is The Default!", done);
})
});
npm test
again, and the test should pass again (which proves that your code still works even though it’s not connecting to Split just yet).on
and off
. Click Save changes and then Confirm.process.env.API_KEY
. We need to set this value in your Heroku app’s environment. Fortunately, this is super easy to do. In your Split admin console, you need to locate your API key. Click on the square tile in the upper left. Then click Admin settings. Click API keys. You want to copy the correct Server-side API key. In my case, I set up my split in the Prod-Default
environment. Click Copy next to the correct Key. From your terminal window, execute:heroku config:set SPLIT_API_KEY=<paste in key>
ProTip: You can monitor the progress of your pipeline on the Actions tab of your GitHub repo. You can also watch the log output of the Heroku app with: heroku logs -t
.
curl https://micah-simple-node-api.herokuapp.com`
Hello, Your Treatment is OFF!
on
. Once it’s been turned on, swing over to your command line and execute curl once again.curl https://micah-simple-node-api.herokuapp.com
Hello, Your Treatment is ON!
Actions
tab.