Node.js is one of the most exciting languages to come to the developer community in the last decade - taking the widely adopted and easy to learn language of JavaScript and allowing developers to build webservers, networking tools and interact with the filesystem. It's a super-versatile language!
Let's see some practical uses of Travis CI and Node.js/Express!
The first thing you want to do is set up a quick package.json
we can do this by opening the terminal and making some directories:
mkdir myapp
cd myapp
Now let's set up the package.json
by using npm init
. Check out this for more information on npm init.
npm init -y
Next in this Node project - we'll be using Express. So again let's keep the terminal open and run:
npm install express --save
For some of our development deps, we will want to grab supertest jest
you can do this by running:
npm install supertest jest --save-dev
Let's say we have program that's just some classic Express:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', async (req, res) => res.status(200).send('Hello World!'));
app.listen(port, () => console.log(`Our app listening on port ${port}!`));
This works for manual testing, we don't want that per se, we want to automate this process, so let's tinker with the original app:
const express = require('express');
const app = express();
app.get('/', async (req, res) => res.status(200).send('Hello World!'));
module.exports = app; // <--
So you might be asking, how do we launch the app? We will use SOC or Seperations of Concern. We place the call to => Listen()
in a file called server.js
. Another great resource are to use something called Lifecycle Scripts. These can be really helpful when setting the foundation of your project.
Make sure not to name this something like express.js
, but server.js
:
const app = require('./app');
const port = 3000;
app.listen(port, () => console.log(`Our app listening on port ${port}!`))
Depending on the permisisons, which we can change with chmod
, we can now run:
node server.js
Make sure we add this to our package.json
- this will be an issue say is somebody forks this, and tries to use npm start
. Fundamentally, package.json
is a metafile for your application. It lists all the configuration of your application. The more complex/tiresome of procuring a package.json
file is running npm init
. In this example though, we did use init
.
We will want to add this to our package.json
"scripts": {
"start": "node server.js"
},
We'll want to run local tests before we setup Travis! We'll be setting up supertest
:
const app = require('../app');
const request = require('supertest');
describe('GET /', () => {
it('responds with 200', async () => {
await request(app)
.get('/')
.expect(200);
});
})
In a quick sense, supertest
will make mock requests to the app. Mocking
is faster when it boils down to it a lot more par for the course when a server is running on say localhost
.
We'll need to repeat a step, and add this to our package.json
:
"scripts": {
"start": "node server.js",
"test": "jest"
},
Remember the aliases
can be anything, but start
and test
are the easiest to remember, and are reliable.
You can now test it locally using npm test
.
Pushing into a repository
Perfect, now we want to push this code to GitHub, using the classic flow:
git init
git add .
git commit -m "Our Express Project"
git remote add origin remote repository URL
git remote -v
git push -u origin master
Now you have a repo say entitled express-app
, it's time to implement Travis, as you know we do this with the .travis.yml
file. Add the following .travis.yml
file to your project:
language: node_js
node_js:
- lts/*
LTS
stands for 'Long Term Support' just if you were wondering! In this case Travis will use npm test
aliases, but Travis is still crucial to make sure your project doesn't break somewhere. You can also run:
npm run lint
Make sure your account is synced up with Travis, and now your build will pass and anytime you make changes, you now have your CI/CD setup for your Node/Express project. It's that easy.
Cookbook Series
We have new recipes every other week, make sure you come back for a practical way of using Travis for beginners.
Top comments (0)