<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Thieves Like Us]]></title><description><![CDATA[Opinions and code. But mostly opinions.]]></description><link>http://brianmajewski.com/</link><generator>Ghost 0.6</generator><lastBuildDate>Wed, 08 Apr 2026 10:06:39 GMT</lastBuildDate><atom:link href="http://brianmajewski.com/tag/npm/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[(Re)Learning Backbone Part 3]]></title><description><![CDATA[<p><strong>Project Structure</strong></p>

<p>We are going to start with a simple barebones structure to start with. In your project directory create both an <code>app</code> and a <code>public</code> directory. Under, <code>public</code>, create <code>assets</code>. And we're done. We'll add more as we flesh out the app. I like working iteratively, adding what we</p>]]></description><link>http://brianmajewski.com/2015/02/15/relearning-backbone-part-3/</link><guid isPermaLink="false">d22b402c-21a2-4fb1-81a8-25b81e6e0c73</guid><category><![CDATA[backbone]]></category><category><![CDATA[javascript]]></category><category><![CDATA[node]]></category><category><![CDATA[npm]]></category><category><![CDATA[express]]></category><category><![CDATA[nodemon]]></category><dc:creator><![CDATA[Brian Majewski]]></dc:creator><pubDate>Sun, 15 Feb 2015 20:30:19 GMT</pubDate><media:content url="http://brianmajewski.com/content/images/2015/02/p1010298--2-.jpg" medium="image"/><content:encoded><![CDATA[<img src="http://brianmajewski.com/content/images/2015/02/p1010298--2-.jpg" alt="(Re)Learning Backbone Part 3"><p><strong>Project Structure</strong></p>

<p>We are going to start with a simple barebones structure to start with. In your project directory create both an <code>app</code> and a <code>public</code> directory. Under, <code>public</code>, create <code>assets</code>. And we're done. We'll add more as we flesh out the app. I like working iteratively, adding what we need when we need it. Remember, it's <em>software</em> - we can change it if we want to.</p>

<p><img src="http://brianmajewski.com/content/images/2015/02/Screen-Shot-2015-02-15-at-11-38-14-AM.png" alt="(Re)Learning Backbone Part 3"></p>

<p><strong>NPM (Node Package Manager)</strong></p>

<p>To manage our server side dependencies, we are going to use Node's built-in package manager <a href="https://www.npmjs.com/">NPM</a>. To start, we need a <code>package.json</code> file. You can read the docs and create one by hand or you can use the tool to create one. Run  </p>

<pre><code>npm init</code></pre>

<p>in the root of your project, answer the questions, <em>et voila</em>, you should have a <code>package.json</code> file that looks something like this:</p>

<p><img src="http://brianmajewski.com/content/images/2015/02/Screen-Shot-2015-02-15-at-11-48-58-AM.png" alt="(Re)Learning Backbone Part 3"></p>

<p>If you entered something other than <code>server.js</code> for the <em>main</em> value (and you probably did, since it isn't the default), go ahead and edit the contents. <code>server.js</code> will be the main file we run to launch our web server and serve up data.</p>

<p>For our barebones server, we are going to use the following node packages: <a href="http://expressjs.com/">express</a>, <a href="https://www.npmjs.com/package/body-parser">body-parser</a>, <a href="https://www.npmjs.com/package/morgan">morgan</a>, and live</p>

<dl>  
<dt>express</dt>  
<dd>will provide nearly all of the web server functionality along with the middleware we will need to do authentication.</dd>  
<dt>body-parser</dt>  
<dd>will allow us to read json from incoming request</dd>  
<dt>morgan</dt>  
<dd>will let us log incoming requests</dd>  
</dl>

<p>Learning how to use Express would be a lesson in itself and you should dig into their documentation as needed.</p>

<p>To install these, run  </p>

<pre><code>npm install express body-parser morgan --save</code></pre>

<p>from your project root. It will download the packages, and their dependencies, and save the information to your package.json file.</p>

<p>One last package we want to download is <a href="https://www.npmjs.com/package/nodemon">nodemon</a>. When our server is running, this will watch for any changes and restart the server automatically as needed. We need to install this globally, so run:</p>

<pre><code>npm install -g nodemon</code></pre>

<p><strong>server.js</strong></p>

<p>This will be the heart of our web server.</p>

<pre><code class="language-javascript">var express = require('express');  
var bodyParser = require('body-parser');  
var morgan = require('morgan');

var path = require('path');

var app = express();

// Sets us up to read body content from POST requests
app.use(bodyParser.urlencoded({extended: true}));  
app.use(bodyParser.json());

// Adds request logging
app.use(morgan('dev'));

// Sets up the server to serve static files from the public directory
app.use(express.static(__dirname + '/public'));

// For all requests we do not explicitly handle (*), return index.html
app.get('*', function(req,res){  
    res.sendFile(path.join(__dirname + '/public/index.html'));
});


// Listen on port 1337
var port = 1337;  
app.listen(port);  
console.log('Server up and running on port ' + port);  
</code></pre>

<p>So, what's going on here? </p>

<ul>
<li>First we define our dependencies and create an <code>app</code> variable to represent the application/server.</li>
<li>Then, we use the <em>body-parser</em> package to tell our app how to get JSON from the body of incoming requests. </li>
<li>We activate <em>morgan</em> in dev mode, which will log our requests.</li>
<li>We define a directory to serve up static resources like html, css, fonts, etc. <code>__dirname</code> is provided by the <em>path</em> package.</li>
<li>Finally, for all incoming requests that have not been handled yet(<code>*</code>), we return <code>index.html</code>. So, if we ask for a specific CSS file, the static instruction above will handle it. If we ask for an HTML page that doesn't exist, this instruction will handle it. We will be adding more routes and instructions as we go on. As you can see, the order in which we define these routes is important. Express will go through each one until it comes to a handler that satisifes the request. This is a classic implementation of the <a href="http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">chain of responsibility</a> pattern.</li>
</ul>

<p>That's it. That's all you need to have a basic web server. We could have left out the body-parser section for an even leaner file, but we will need it next. Run  </p>

<pre><code>nodemon server.js</code></pre>

<p>and you should see output similar to: <br>
<img src="http://brianmajewski.com/content/images/2015/02/Screen-Shot-2015-02-15-at-12-19-18-PM.png" alt="(Re)Learning Backbone Part 3"></p>

<p>We can create a simple <code>index.html</code> file in our public directory and view it in a browser. To ensure that static files are being served up as well, create a <code>css</code> folder under <code>assets</code> and add a <code>styles.css</code> file.</p>

<p><em>Sample HTML</em>  </p>

<pre><code class="language-markup">&lt;!DOCTYPE html&gt;  
&lt;html&gt;  
&lt;head lang="en"&gt;  
    &lt;base href="/"&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Relearning Backbone&lt;/title&gt;
    &lt;link rel="stylesheet" href="assets/css/styles.css"&gt;
&lt;/head&gt;  
&lt;body&gt;  
&lt;h3&gt;Relearning Backbone&lt;/h3&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
</code></pre>

<p><em>Sample CSS</em>  </p>

<pre><code class="language-css">h3 {  
    color: #4169e1;
}
</code></pre>

<p><strong>Next Steps</strong></p>

<p>Next time, we are going to add a database connection to the server, and some User entities to it.</p>]]></content:encoded></item></channel></rss>