<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paul Gordon</title>
    <description>The latest articles on DEV Community by Paul Gordon (@paultravisci).</description>
    <link>https://dev.to/paultravisci</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F33841%2Febf8bfa9-fef9-4534-b7c2-165fde6c256d.jpeg</url>
      <title>DEV Community: Paul Gordon</title>
      <link>https://dev.to/paultravisci</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/paultravisci"/>
    <language>en</language>
    <item>
      <title>Using Node.js/Express with Travis CI</title>
      <dc:creator>Paul Gordon</dc:creator>
      <pubDate>Fri, 30 Oct 2020 12:02:28 +0000</pubDate>
      <link>https://dev.to/travisci/using-node-js-express-with-travis-ci-32ha</link>
      <guid>https://dev.to/travisci/using-node-js-express-with-travis-ci-32ha</guid>
      <description>&lt;p&gt;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!&lt;/p&gt;

&lt;p&gt;Let's see some practical uses of Travis CI and Node.js/Express!&lt;/p&gt;

&lt;p&gt;The first thing you want to do is set up a quick &lt;code&gt;package.json&lt;/code&gt; we can do this by opening the terminal and making some directories:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;myapp
&lt;span class="nb"&gt;cd &lt;/span&gt;myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's set up the &lt;code&gt;package.json&lt;/code&gt; by using &lt;code&gt;npm init&lt;/code&gt;. &lt;a href="https://docs.npmjs.com/cli/v6/commands/npm-init"&gt;Check out this for more information on npm init.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next in this Node project - we'll be using Express. So again let's keep the terminal open and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;express &lt;span class="nt"&gt;--save&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For some of our development deps, we will want to grab &lt;code&gt;supertest jest&lt;/code&gt; you can do this by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;supertest jest &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say we have program that's just some classic Express:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();
const port = 3000;

app.get('/', async (req, res) =&amp;gt; res.status(200).send('Hello World!'));

app.listen(port, () =&amp;gt; console.log(`Our app listening on port ${port}!`));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();

app.get('/', async (req, res) =&amp;gt; res.status(200).send('Hello World!'));

module.exports = app; // &amp;lt;--
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you might be asking, how do we launch the app? We will use SOC or &lt;a href="https://en.wikipedia.org/wiki/Separation_of_concerns"&gt;Seperations of Concern&lt;/a&gt;. We place the call to =&amp;gt; &lt;code&gt;Listen()&lt;/code&gt; in a file called &lt;code&gt;server.js&lt;/code&gt;. Another great resource are to use something called &lt;a href="https://docs.npmjs.com/cli/v6/using-npm/scripts"&gt;Lifecycle Scripts&lt;/a&gt;. These can be really helpful when setting the foundation of your project.&lt;/p&gt;

&lt;p&gt;Make sure not to name this something like &lt;code&gt;express.js&lt;/code&gt;, but &lt;code&gt;server.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const app = require('./app');
const port = 3000;

app.listen(port, () =&amp;gt; console.log(`Our app listening on port ${port}!`))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on the permisisons, which we can change with &lt;code&gt;chmod&lt;/code&gt;, we can now run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure we add this to our &lt;code&gt;package.json&lt;/code&gt; - this will be an issue say is somebody forks this, and tries to use &lt;code&gt;npm start&lt;/code&gt;. Fundamentally, &lt;code&gt;package.json&lt;/code&gt; is a metafile for your application. It lists all the configuration of your application. The more complex/tiresome of procuring a &lt;code&gt;package.json&lt;/code&gt; file is running &lt;code&gt;npm init&lt;/code&gt;. In this example though, we did use &lt;code&gt;init&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We will want to add this to our &lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "node server.js"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll want to run local tests before we setup Travis! We'll be setting up &lt;code&gt;supertest&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const app = require('../app');
const request = require('supertest');

describe('GET /', () =&amp;gt; {

    it('responds with 200', async () =&amp;gt; {
        await request(app)
            .get('/')
            .expect(200); 
    });
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a quick sense, &lt;code&gt;supertest&lt;/code&gt; will make mock requests to the app. &lt;code&gt;Mocking&lt;/code&gt; is faster when it boils down to it a lot more par for the course when a server is running on say &lt;code&gt;localhost&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We'll need to repeat a step, and add this to our &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "node server.js",
  "test": "jest"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember the &lt;code&gt;aliases&lt;/code&gt; can be anything, but &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;test&lt;/code&gt; are the easiest to remember, and are reliable. &lt;/p&gt;

&lt;p&gt;You can now test it locally using &lt;code&gt;npm test&lt;/code&gt;. &lt;/p&gt;

&lt;h1&gt;
  
  
  Pushing into a repository
&lt;/h1&gt;

&lt;p&gt;Perfect, now we want to push this code to GitHub, using the classic flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init
git add &lt;span class="nb"&gt;.&lt;/span&gt; 
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Our Express Project"&lt;/span&gt; 
git remote add origin remote repository URL
git remote &lt;span class="nt"&gt;-v&lt;/span&gt; 
git push &lt;span class="nt"&gt;-u&lt;/span&gt; origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have a repo say entitled &lt;code&gt;express-app&lt;/code&gt;, it's time to implement Travis, as you know we do this with the &lt;code&gt;.travis.yml&lt;/code&gt; file. Add the following &lt;code&gt;.travis.yml&lt;/code&gt; file to your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;node_js&lt;/span&gt;
&lt;span class="na"&gt;node_js&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
 &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;lts/*&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;LTS&lt;/code&gt; stands for 'Long Term Support' just if you were wondering! In this case Travis will use &lt;code&gt;npm test&lt;/code&gt; aliases, but Travis is still crucial to make sure your project doesn't break somewhere. You can also run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run lint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cookbook Series
&lt;/h2&gt;

&lt;p&gt;We have new recipes every other week, make sure you come back for a practical way of using Travis for beginners. &lt;/p&gt;

</description>
      <category>devops</category>
      <category>node</category>
      <category>javascript</category>
      <category>express</category>
    </item>
  </channel>
</rss>
