<?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: da club has asbestos</title>
    <description>The latest articles on DEV Community by da club has asbestos (@jukebapes).</description>
    <link>https://dev.to/jukebapes</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%2F199084%2F4d0025dc-baf8-47b8-9699-b206c586c028.jpg</url>
      <title>DEV Community: da club has asbestos</title>
      <link>https://dev.to/jukebapes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jukebapes"/>
    <language>en</language>
    <item>
      <title>Containerized ExpressJS service in 10 mins or less</title>
      <dc:creator>da club has asbestos</dc:creator>
      <pubDate>Mon, 06 Jan 2020 02:25:30 +0000</pubDate>
      <link>https://dev.to/jukebapes/containerized-expressjs-service-in-10-mins-or-less-4n80</link>
      <guid>https://dev.to/jukebapes/containerized-expressjs-service-in-10-mins-or-less-4n80</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Yah this article has probably been written 100 times across different blogs. However there are two reasons why I am writing it: &lt;/p&gt;

&lt;p&gt;1.) I'm trying to get into blogging and sharing my knowledge this year. &lt;/p&gt;

&lt;p&gt;2.) I am trying to make this something a complete beginner could follow,and be running in 10 mins or less.      &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what are we going to build here&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will be setting up a simple NodeJS server (powered by ExpressJS). &lt;br&gt;
This server will support a API, along with a view engine for rendering some pretty things to the browser. Finally, we will wrap our application in a Docker container allowing easy hosting in a service like AWS. &lt;/p&gt;
&lt;h1&gt;
  
  
  Glossary (list of tools and concepts you need to know)
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://nodejs.org/en/"&gt;NodeJS&lt;/a&gt;&lt;br&gt;
A javascript runtime that allows javascript code to be written and executed outside of a browser. It come with a set of standard utility libraries for handling file system operation, compression, and other low level tasks. It allows us to build backend servers in Javascript.   &lt;/p&gt;

&lt;p&gt;&lt;a href="https://expressjs.com/"&gt;ExpressJS&lt;/a&gt;&lt;br&gt;
A popular web framework that runs on top of NodeJS. It handles HTTP calls, routing, views, etc. Basically it abstracts away some of the low level stuff so that we can focus on building websites. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt;&lt;br&gt;
This is a popular software for running containers. Containers are operating systems that run as a virtualized instance on your machine (or on a cloud host). Since the docker environment is decoupled from your host machine, you have confidence that your software will run the same anywhere you decide to put it. &lt;/p&gt;
&lt;h1&gt;
  
  
  Step1: Setting up a ExpressJS server
&lt;/h1&gt;

&lt;p&gt;First you will need to install NodeJS. If you are on OSX, the easiest way to do this is via a package manager like &lt;a href="https://brew.sh/"&gt;Homebrew&lt;/a&gt;. This allows you open your terminal and run &lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew install nodejs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;otherwise just install it manually from &lt;a href="https://nodejs.org/en/"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Once node is setup we can bootstrap our ExpressJS project. The easiest way to do this is to use a tool called &lt;a href="https://expressjs.com/en/starter/generator.html"&gt;express-generator&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Install this as a global node package using&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install -g express-generator&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once that finishes we run a command to setup our project&lt;/p&gt;

&lt;p&gt;&lt;code&gt;express --view=ejs my-app&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This sets up a new express app, using the &lt;a href="https://ejs.co/"&gt;EJS&lt;/a&gt; view engine. Putting it into a folder called my-app. &lt;/p&gt;

&lt;p&gt;Navigate to the my-app folder and run &lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will parse the package.json and install an necessary dependencies. &lt;/p&gt;

&lt;p&gt;With this you should be able to type &lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm run start&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Navigate to &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; to see your app. By default there are two routes active. &lt;/p&gt;

&lt;p&gt;&lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; -&amp;gt; Index/Title Page &lt;br&gt;
&lt;a href="http://localhost:3000/users"&gt;http://localhost:3000/users&lt;/a&gt; -&amp;gt; A api page that returns a mock resource. &lt;/p&gt;

&lt;p&gt;Im not going to go in depth here on how to build a full application here. Feel free to explore/experiment with this: &lt;/p&gt;

&lt;p&gt;app.js -&amp;gt; This is your server entry point. High level route registration, and middleware injection happens here. &lt;/p&gt;

&lt;p&gt;views/ -&amp;gt; this folder holds your UI templates &lt;br&gt;
routes/ -&amp;gt; this holds the javascript route controllers. They tell your server what to do when a user tries to hit a specific page &lt;br&gt;
public/ -&amp;gt; This holds your static assets CSS/Javascript/Images &lt;/p&gt;
&lt;h1&gt;
  
  
  Step 2: Running above code in Docker
&lt;/h1&gt;

&lt;p&gt;First we need to install docker. Unfortunately this is a rare case where its actually more annoying to use Homebrew than it is to just get the software from the official website. Download &lt;a href="https://www.docker.com/get-started"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Once you have Docker installed we need to make 2 files in our &lt;code&gt;/my-app/&lt;/code&gt;directory.  &lt;/p&gt;

&lt;p&gt;1.) Create a &lt;code&gt;.dockerignore&lt;/code&gt; file and the following lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node_modules
npm-debug.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this prevents docker from packaging our dependencies. It will download those itself, using npm, &lt;/p&gt;

&lt;p&gt;2.) Next create a file called &lt;code&gt;Dockerfile&lt;/code&gt; at the root of &lt;code&gt;/my-app/&lt;/code&gt;&lt;br&gt;
Read the comments here if you are curious what each line means&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# We import our base operating image. In this case I use node:12 
# as that is the most recent stable release of NodeJS
from node:12


# Create a directory where your app will run, inside the docker environment
WORKDIR /usr/src/app

# Copy package.json and package-lock.json these are your depedency definitions 
COPY package*.json ./

# Install your dependencies 
RUN npm install 

# Copy your application into the container directory 
COPY . . 

# Expose whatever port your application is running on. 
# In this case it is port 3000
EXPOSE 3000

# Start our application
CMD ["npm", "run", "start"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this we have defined what our Docker container will do. Now it is time to build it. Run the following while in the directory where your Dockerfile is located: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker build -t my-app-container .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will execute the above Dockerfile. Setup all dependencies, and build a executable image. &lt;/p&gt;

&lt;p&gt;once this is complete run &lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker images&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;you should see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-app-container    latest              c4f59b39a747        9 minutes ago       915MB
node                12                  6b5991bf650f        8 days ago          913MB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to run your app in the container you can run this command: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -p 8080:3000 my-app-container&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command runs the images, and forwards traffic from the internal docker port (3000 where our app is running), to the local port 8080. &lt;/p&gt;

&lt;p&gt;With this you should be able to navigate to http:localhost:8080, and see the express app running. &lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;That it you now have a express app setup, and a portable Docker container. You can run this container locally, put it on a service like Amazon ECS, Digital Ocean, Azure, Google Container Service. Or just give it to a friend, and you will be sure that it runs exactly as on your machine. &lt;/p&gt;

&lt;h1&gt;
  
  
  Next Steps
&lt;/h1&gt;

&lt;p&gt;Of course the app we made is far from production ready. There are a couple things we could do to improve the overall architecture here. For example. &lt;/p&gt;

&lt;p&gt;1.) Add a NGINX layer for to act as a load balancing production server. &lt;/p&gt;

&lt;p&gt;2.) Add Supervisor to auto restart our app process on failure.&lt;/p&gt;

&lt;p&gt;3.) Add Typescript to express for type safe code. &lt;/p&gt;

&lt;p&gt;4.) Serve a ReactJS bundle on one of our routes. &lt;/p&gt;

&lt;p&gt;5.) Deploy our app to a cloud provider like Amazon ECS &lt;/p&gt;

&lt;p&gt;6.) Add nodemon to speed up our development workflow. &lt;/p&gt;

&lt;p&gt;Comment if any of these would interest you in future tutorials. &lt;/p&gt;

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