<?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: alexandertrout</title>
    <description>The latest articles on DEV Community by alexandertrout (@alexandertrout).</description>
    <link>https://dev.to/alexandertrout</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%2F289226%2F59803b94-19d7-40e9-ab83-f7952c851d75.jpg</url>
      <title>DEV Community: alexandertrout</title>
      <link>https://dev.to/alexandertrout</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexandertrout"/>
    <language>en</language>
    <item>
      <title>Coding Bootcamp - Backend #3 - Databases, SQL, and knex</title>
      <dc:creator>alexandertrout</dc:creator>
      <pubDate>Fri, 07 Feb 2020 14:29:22 +0000</pubDate>
      <link>https://dev.to/alexandertrout/coding-bootcamp-backend-3-databases-sql-and-knex-2p24</link>
      <guid>https://dev.to/alexandertrout/coding-bootcamp-backend-3-databases-sql-and-knex-2p24</guid>
      <description>&lt;h4&gt;
  
  
  What is a Relational Database?
&lt;/h4&gt;

&lt;p&gt;Relational databases are easy to think of as tables, like you would find in excel, they have descriptive column headers and each new row represents a different case. The relational aspect comes in when we start matching tables up by rows they have in common.&lt;/p&gt;

&lt;p&gt;This is easier to think of with an example, if we had one table of movies with movie_id, name, director, and rating.&lt;br&gt;
It’s likely that we would have multiple films in that table with the same director, in this example ‘x’.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;film_id&lt;/th&gt;
&lt;th&gt;director_name&lt;/th&gt;
&lt;th&gt;rating&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;y&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;z&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is when we could use another table to store all our director data. These tables can then be ‘joined’ together to avoid repeating data, and to store more extra information about directors separately. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Change the director name from the table above to a director_id instead. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A second table will have a director_id column and contain information about each director on a new row (and therefore a new director_id).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The id in both tables must match correctly in order to join them by that column. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After joining, because the two tables have a column in common, they will each have access to data from the other. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Shown below is what you could expect to have access to after joining two tables of film data and director data. This style of database, using multiple tables to store separate data and giving them unique id’s to perform joins, is called a relational database.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;film_id&lt;/th&gt;
&lt;th&gt;director_id&lt;/th&gt;
&lt;th&gt;rating&lt;/th&gt;
&lt;th&gt;director_name&lt;/th&gt;
&lt;th&gt;age&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;52&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;y&lt;/td&gt;
&lt;td&gt;33&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;52&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;z&lt;/td&gt;
&lt;td&gt;45&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h4&gt;
  
  
  Migrations and Seeding
&lt;/h4&gt;

&lt;p&gt;Migrations are files which are run to create the initial tables in a database. They declare the column headers, data type for each column and certain rules the columns must follow. An example of this is that some columns MUST contain data, and can therefore be given a NOT NULL property. Seeding is the action of populating the empty tables that have been created with data.&lt;/p&gt;

&lt;p&gt;Migrations and seed files can be automated  with mocha testing hooks to be run before each test, this makes developing a larger testing suite for your server easier as you don't need to keep track of what you've already edited in the database every time you run a test.&lt;/p&gt;
&lt;h4&gt;
  
  
  Querying Databases
&lt;/h4&gt;

&lt;p&gt;Server ✅&lt;br&gt;
Populated Database ✅ &lt;br&gt;
Test file ✅&lt;/p&gt;

&lt;p&gt;It’s time to make a GET request! &lt;/p&gt;

&lt;p&gt;Servers need to communicate with databases to fetch information for the end user (or the front end developer). This communication is called querying. Querying a database would take place in the models files, and after receiving a response from the database the data is returned into the controller files to be sent.&lt;/p&gt;

&lt;p&gt;GET request -&amp;gt; API router -&amp;gt; correct controller -&amp;gt; invokes model -&amp;gt; fetches data from database -&amp;gt; returns data to controller -&amp;gt; sends data in the response body.&lt;/p&gt;
&lt;h4&gt;
  
  
  SQL and knex.js
&lt;/h4&gt;

&lt;p&gt;The good news is you can create a database, populate it with data and make requests to that database in your server models all using SQL or knex (which actually still uses SQL). A good online resource for learning some SQL is &lt;a href="https://sqlbolt.com/" rel="noopener noreferrer"&gt;https://sqlbolt.com/&lt;/a&gt;, and once you’ve got a solid understanding of SQL you can move on to knex &lt;a href="http://knexjs.org/" rel="noopener noreferrer"&gt;http://knexjs.org/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9cslyadwvr1hzcge78ok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9cslyadwvr1hzcge78ok.png" alt="Knex"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The docs for knex are very good once you know SQL, the knex syntax is similar so by searching the docs page you can quite easily follow the examples to implement knex instead. Benefits of using knex are that it makes code more readable then using pure SQL, and can work with both callbacks and promise based async flows. &lt;/p&gt;
&lt;h4&gt;
  
  
  knex.js migrations
&lt;/h4&gt;

&lt;p&gt;This is what a basic migrations file looks like for the films table described above. The two main sections are exports.up, which will initialise the table, and exports.down, which will remove it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;up&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;films&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;film_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;primary&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;notNullable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;director_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;references&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;director_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;directors&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notNullable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;down&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dropTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;films&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see it contains readable methods like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;.createTable () ... self explanatory right??&lt;/li&gt;
&lt;li&gt;.text('title') which will create a column with header of title and data type of text&lt;/li&gt;
&lt;li&gt;.increments(film_id) which will create a colum with header of film_id and will auto increase the id by 1 when each row is added&lt;/li&gt;
&lt;li&gt;.notNullable() which dictates that the column must contain data or an error will be thrown.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a brief example of some of the methods avaliable, and far more complex tables will require larger exports functions for adding more columns, and more restrictive rules on data types for example. The good news is methods are chain-able (as in the case of director_id below, i've chained a few methods together to create that column). Have a look at the docs and see what tables you can manage to create! &lt;/p&gt;

&lt;h4&gt;
  
  
  knex.js seeding
&lt;/h4&gt;

&lt;p&gt;This is what a basic seeding file looks like for the films table. &lt;br&gt;
It's important to .rollback() (deleting the tables) and .migrate.latest() (re-initialising them empty) before inserting your data. This is shown in the example below using promises to ensure the order is followed correctly. The actual insertion of data is simple and uses the .insert() method, with .returning('*') chained to show us the data we've inserted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;seed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;migrate&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rollback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;migrate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;latest&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;films&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filmData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;returning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt; 
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once your first table is populated you can chain more .then() blocks to add data to other tables, this can get trick though as you may have to edit your data to correctly format the director_id columns so they match and the joining will go smoothly.&lt;/p&gt;

&lt;h4&gt;
  
  
  knex.js querying
&lt;/h4&gt;

&lt;p&gt;This is what querying the database above would look like in a server model called fetchFilmById, this would be invoked by a controller and pass the received data back up to the controller to be sent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fetchFilmById&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;film_id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;films&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;film_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;film_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This model takes one argument, the film_id the user wants to get data about (this would usually be passed in as a parameter in the request.If the film_id matched correctly, a films data (that row of the table) would be returned as an object and sent to the user.&lt;/p&gt;

&lt;h4&gt;
  
  
  Full examples
&lt;/h4&gt;

&lt;p&gt;An example of a full backend project with migrations, seeding, lots of endpoints and more complex querying can be seen on my github repo &lt;a href="https://github.com/alexandertrout/alex-be-nc-news" rel="noopener noreferrer"&gt;https://github.com/alexandertrout/alex-be-nc-news&lt;/a&gt; - please get in touch if you have any questions regarding what it's like to attend a coding bootcamp, or any of the things I've tried to cover in this article.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>Coding Bootcamp - Backend #2 - Servers, Express &amp; Promises</title>
      <dc:creator>alexandertrout</dc:creator>
      <pubDate>Sat, 25 Jan 2020 14:46:39 +0000</pubDate>
      <link>https://dev.to/alexandertrout/coding-bootcamp-backend-2-servers-express-promises-637</link>
      <guid>https://dev.to/alexandertrout/coding-bootcamp-backend-2-servers-express-promises-637</guid>
      <description>&lt;h4&gt;
  
  
  What is a server?
&lt;/h4&gt;

&lt;p&gt;Servers will deal with requests made by clients to your API over the internet, requests being made by using standard web http protocols. Different 'endpoints' on the API are made avaliable to users, and methods like GET, POST, and DELETE are used on these different endpoints. RESTful servers will follow this pattern, with multiple methods being allowed on each endpoint instead of having a different endpoint for each method. For example, a GET request on the /api/users endpoint should simply respond to the client with information about all the users. However, this endpoint may also allow a POST request, to insert a new user to the database. This ability to have multiple methods allowed on one endpoint is important, and can be implemented by using express to build your server...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1CVbukPd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://buttercms.com/static/images/tech_banners/ExpressJS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1CVbukPd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://buttercms.com/static/images/tech_banners/ExpressJS.png" alt="Express" width="880" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I build a server using express?
&lt;/h4&gt;

&lt;p&gt;Express is a web framework for Node.js which allows users to create apps which will listen for requests from clients and will send responses. More information on express and getting started can be found here &lt;a href="https://expressjs.com/"&gt;https://expressjs.com/&lt;/a&gt;. Using express to build a simple server can be done quickly, and then scaled up to add routers and multiple endpoints, each allowing different methods.&lt;/p&gt;

&lt;p&gt;MVC or Model - Controller - View is the software design pattern I've been following. Essentially, once routing has been used to reach an endpoint a 'controller' will handle the request and response, and a 'model' will handle contacting another API or the database, perform any necessary logic, and will return a promise to the controller to be handled. The V in MVC stand for 'view' - this is the point where pages can be served to the user containing the data they requested, and is achieved through using EJS, PUG or similar to inject the data into a HTML file. This approach is still used by many companies, but it has become more popular to remove this presentation aspect to a front-end framework such as react instead.&lt;/p&gt;

&lt;p&gt;Servers can look complicated at first... &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/PD9hjqdeidgqY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/PD9hjqdeidgqY/giphy.gif" alt="How servers work" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I'd recommend following a tutorial for a simple server to begin with and build upon that, adding more complexity slowly. Heres one I've made reference to a few times that will explain the basic concepts well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=L72fhGm1tfE&amp;amp;t=1527s"&gt;https://www.youtube.com/watch?v=L72fhGm1tfE&amp;amp;t=1527s&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  How do promises make async programming easier?
&lt;/h4&gt;

&lt;p&gt;As mentioned in the last post, async programming must be used to avoid blocking when writing code. This was managed previously by using callback functions for any of our asynchronous code. Promises simplify this process by returning what is essentially a 'black box' from a function. This returned 'promise' can be in one of three states: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Fulfilled: resolve() was called (Some data is returned inside the promise).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rejected: reject() was called (Something went wrong and an error was returned inside the promise).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pending: not yet fulfilled or rejected.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Promises can wrap functionality in the same way that asynchronous callbacks do, and gaining access to the values within the promises is possible by using the .then() and .catch() methods, once the promise has resolved or rejected. &lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;getData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* data request from an external source */&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.then()&lt;/strong&gt; &lt;br&gt;
Using a .then() after a promise allows us to gain access to the promises return value within the parentheses, shown below is an example where the promise has fulfilled and our data would be output to the console. In place of this console log is where we could place functionality regarding that data including returning that data from our API to a client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.catch()&lt;/strong&gt;&lt;br&gt;
Similarly to using an error first callback, a .catch(next) after a promise will invoke the next error handling middleware function in the chain and respond with an error message explaining what went wrong. So using the example below, if there was an error getting the data the promise would be rejected with an error, and next would invoke the next error handler with that error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So using promises can streamline building a server using express, and will hopefully make your code easier to follow. Once you've got a grip on using promises regularly they become easy to implement and will ultimately make your backend code better. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples of some of my apis can be found on my gitHub:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/alexandertrout/songgame"&gt;https://github.com/alexandertrout/songgame&lt;/a&gt; - A song guessing game that uses express and EJS to present views to the user. This game is also hosted on heroku and can be played at &lt;a href="https://pickthefakesong.herokuapp.com/"&gt;https://pickthefakesong.herokuapp.com/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/alexandertrout/alex-be-nc-news"&gt;https://github.com/alexandertrout/alex-be-nc-news&lt;/a&gt; - A more complex, reddit style backend that will allow users to post articles, comments and votes. &lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>bootcamp</category>
    </item>
    <item>
      <title>Coding Bootcamp - Backend #1 - Asynchronous code</title>
      <dc:creator>alexandertrout</dc:creator>
      <pubDate>Tue, 21 Jan 2020 07:20:34 +0000</pubDate>
      <link>https://dev.to/alexandertrout/coding-bootcamp-backend-1-asynchronous-code-2c9l</link>
      <guid>https://dev.to/alexandertrout/coding-bootcamp-backend-1-asynchronous-code-2c9l</guid>
      <description>&lt;p&gt;A quick recap, After the first 3 week block I'm comfortable using the command line, Git, GitHub, HTML, CSS, and JavaScript. I've been taught concepts such as closure, recursion, functional programming, and using classes in OOP. I've also done more katas than I can remember and made some really basic websites in my spare time outside of classes.&lt;/p&gt;

&lt;p&gt;... &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>sql</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Starting a Coding Bootcamp - Fundamentals</title>
      <dc:creator>alexandertrout</dc:creator>
      <pubDate>Mon, 30 Dec 2019 17:40:18 +0000</pubDate>
      <link>https://dev.to/alexandertrout/starting-a-coding-bootcamp-4e77</link>
      <guid>https://dev.to/alexandertrout/starting-a-coding-bootcamp-4e77</guid>
      <description>&lt;h4&gt;
  
  
  The first few weeks of coding.
&lt;/h4&gt;

&lt;p&gt;To start with I was learning HTML and CSS basics, as well as some JavaScript fundamentals on websites like &lt;a href="https://www.codecademy.com/"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.freecodecamp.org/"&gt;FreeCodeCamp&lt;/a&gt;. I’d been doing these online tutorials and katas (online coding challenges) alongside working full-time, so it was quite time consuming but I needed to be sure that coding was for me. Nailing these fundamentals and fully understanding them was also key to getting along easier when more complex ideas would be introduced later.  Once I was confident enough, and after a lot of researching bootcamp graduate outcomes (including a fair amount of LinkedIn stalking), I took the leap and signed up for a bootcamp.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TRS-L28s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://media.licdn.com/dms/image/C511BAQHqArTPxU3gMg/company-background_10000/0%3Fe%3D2159024400%26v%3Dbeta%26t%3D10xyPsaeWAy28faW2xhUhaNnmyGKiuq-fCNjHviubNM" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TRS-L28s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://media.licdn.com/dms/image/C511BAQHqArTPxU3gMg/company-background_10000/0%3Fe%3D2159024400%26v%3Dbeta%26t%3D10xyPsaeWAy28faW2xhUhaNnmyGKiuq-fCNjHviubNM" alt="FreeCodeCamp" width="880" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  So, what's it actually like getting started?
&lt;/h4&gt;

&lt;p&gt;One of the realisations I’ve had so far is I now know how it feels to be coding full-time and it’s easier to get a feel for how that might translate to the workplace. I’ve always preferred to learn quickly, especially when you get straight to putting the concepts into practice. Let's just say doing a bootcamp &lt;strong&gt;is hard work&lt;/strong&gt;, but at least you'll never get bored! &lt;/p&gt;

&lt;p&gt;It’s important to understand you won’t be building apps from scratch on day one, even if you could follow a YouTube tutorial and do this at home. That won't cut it in a job. You're changing careers to do work in what is basically a foreign language in just a few months. There's a lot of times you'll feel like this...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/DfSXiR60W9MVq/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/DfSXiR60W9MVq/giphy.gif" alt="Alan Thinking" width="480" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s about understanding everything from the bottom up; not just being able to code, but about being able to explain why you're doing what you’re doing. One of the trickiest things to get used to is that the answers aren’t always going to be there, and that tutors will only help you so far. In this case, learn to Google. What you'll be told a lot is that devs are basically professional Googlers. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3269mfsCyevQsbZ9qv/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3269mfsCyevQsbZ9qv/giphy.gif" alt="Google It!" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Getting stuck and getting yourself unstuck is a huge part of what makes code enjoyable, if you like to solve problems then you need problems to solve!&lt;/p&gt;

&lt;h4&gt;
  
  
  What Have I Learnt?
&lt;/h4&gt;

&lt;p&gt;So far in the first 3 week block I've been introduced to the command line, Git, GitHub, HTML, CSS, and JavaScript. I feel comfortable with coding concepts such as closure, recursion, functional programming, and using classes in OOP. Everything has been done using test driven development (TDD) - which is something that you should definitely, always, 100%, be using. It's what's used in actual development of production code to make sure everything works as expected, so early exposure to TDD is a great thing for anyone looking for a career in development.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/b7f0X8Okk1uyk/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/b7f0X8Okk1uyk/giphy.gif" alt="Do It!" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Over the next few weeks I'll be covering all things back end. I’m excited to start using my coding skills to work with API’s and real-world data. You can check in on what I'm doing by having a look at &lt;a href="https://github.com/alexandertrout"&gt;my GitHub&lt;/a&gt; account. &lt;/p&gt;

&lt;h4&gt;
  
  
  Key Lessons:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Coding style is IMPORTANT&lt;/li&gt;
&lt;li&gt;Use the command line now so you’re ready for later&lt;/li&gt;
&lt;li&gt;&lt;del&gt;recursion is really easy to understand&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;Closure, recursion and async are difficult to wrap your head around. You need to know them well, so just do the research until you understand! &lt;/li&gt;
&lt;li&gt;You can never do enough katas&lt;/li&gt;
&lt;li&gt;Google.&lt;/li&gt;
&lt;li&gt;TDD.&lt;/li&gt;
&lt;li&gt;Enjoy it, it's exciting to learn how tech works!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>career</category>
      <category>bootcamp</category>
    </item>
  </channel>
</rss>
