-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
193 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,37 @@ | ||
<!DOCTYPE html> <html> <head> <title>app.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> app.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p>Export app for other modules</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">express = </span><span class="nx">require</span><span class="p">(</span><span class="s2">"express"</span><span class="p">)</span> | ||
<span class="nv">app = module.exports = </span><span class="nx">express</span><span class="p">.</span><span class="nx">createServer</span><span class="p">()</span> | ||
<!DOCTYPE html> <html> <head> <title>app.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> app.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p>Create server and export <code>app</code> as a module for other modules to require as a dependency | ||
early in this file</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">express = </span><span class="nx">require</span> <span class="s2">"express"</span> | ||
<span class="nv">app = module.exports = </span><span class="nx">express</span><span class="p">.</span><span class="nx">createServer</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>Module requires</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">conf = </span><span class="nx">require</span> <span class="s1">'./lib/conf'</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <h2>App configurations</h2> | ||
|
||
<span class="nx">app</span><span class="p">.</span><span class="nx">configure</span> <span class="o">-></span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">set</span> <span class="s2">"views"</span><span class="p">,</span> <span class="nx">__dirname</span> <span class="o">+</span> <span class="s2">"/views"</span> | ||
<h3>Global app settings</h3> | ||
|
||
<p><strong>Note</strong>: Notice that we got our session secret from the configuration file. In this file | ||
we can define our configurations as globals or based on the node environment, thus | ||
keeping all the configurable variables centralized instead of being scattered all over.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">app</span><span class="p">.</span><span class="nx">configure</span> <span class="o">-></span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">set</span> <span class="s2">"views"</span><span class="p">,</span> <span class="nx">__dirname</span> <span class="o">+</span> <span class="nx">conf</span><span class="p">.</span><span class="nx">dir</span><span class="p">.</span><span class="nx">views</span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">set</span> <span class="s2">"view engine"</span><span class="p">,</span> <span class="s2">"jade"</span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">express</span><span class="p">.</span><span class="nx">bodyParser</span><span class="p">()</span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">express</span><span class="p">.</span><span class="nx">methodOverride</span><span class="p">()</span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">app</span><span class="p">.</span><span class="nx">router</span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">express</span><span class="p">.</span><span class="nx">static</span><span class="p">(</span><span class="nx">__dirname</span> <span class="o">+</span> <span class="s2">"/public"</span><span class="p">)</span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">express</span><span class="p">.</span><span class="nx">cookieParser</span><span class="p">()</span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">express</span><span class="p">.</span><span class="nx">session</span> | ||
<span class="nv">secret: </span><span class="nx">conf</span><span class="p">.</span><span class="nx">sessionSecret</span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">express</span><span class="p">.</span><span class="nx">static</span><span class="p">(</span><span class="nx">__dirname</span> <span class="o">+</span> <span class="nx">conf</span><span class="p">.</span><span class="nx">dir</span><span class="p">.</span><span class="nx">public</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <h3>Environment based settings</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p><strong>Note</strong>: Express defaults to 'development' environment if $NODE_ENV is not defined. | ||
If you wish to change to any other environment, run <code>export NODE_ENV='myEnv'</code>. | ||
You can define as many environments and configurations as you wish.</p> | ||
|
||
<p>To get the current node environment, in Express use <code>app.settings.env</code>. </p> | ||
|
||
<span class="nx">app</span><span class="p">.</span><span class="nx">configure</span> <span class="s2">"development"</span><span class="p">,</span> <span class="o">-></span> | ||
<p>For example, </p> | ||
|
||
<pre><code>console.log(app.settings.env) | ||
</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="nx">app</span><span class="p">.</span><span class="nx">configure</span> <span class="s2">"development"</span><span class="p">,</span> <span class="o">-></span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">express</span><span class="p">.</span><span class="nx">errorHandler</span><span class="p">(</span> | ||
<span class="nv">dumpExceptions: </span><span class="kc">true</span> | ||
<span class="nv">showStack: </span><span class="kc">true</span> | ||
<span class="p">)</span> | ||
|
||
<span class="nx">app</span><span class="p">.</span><span class="nx">configure</span> <span class="s2">"production"</span><span class="p">,</span> <span class="o">-></span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">express</span><span class="p">.</span><span class="nx">errorHandler</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>Pages</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">app</span><span class="p">.</span><span class="nx">get</span> <span class="s2">"/"</span><span class="p">,</span> <span class="nf">(req, res) -></span> | ||
<span class="nx">res</span><span class="p">.</span><span class="nx">render</span> <span class="s2">"index"</span> | ||
<span class="nv">title: </span><span class="s1">'Express'</span> | ||
|
||
<span class="nx">app</span><span class="p">.</span><span class="nx">listen</span> <span class="mi">3000</span> | ||
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span> <span class="nx">express</span><span class="p">.</span><span class="nx">errorHandler</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <h3>Require routes</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p><strong>Note</strong>: Routes are placed in a seperate file to reduce the clutter of the main <code>app.js</code> file, | ||
<code>app.js</code> should be used for basic app configurations</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">require</span><span class="p">(</span><span class="s1">'./lib/routes'</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <h3>Start server</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nx">app</span><span class="p">.</span><span class="nx">listen</span> <span class="mi">3000</span> | ||
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s2">"Express server listening on port %d in %s mode"</span><span class="p">,</span> <span class="nx">app</span><span class="p">.</span><span class="nx">address</span><span class="p">().</span><span class="nx">port</span><span class="p">,</span> <span class="nx">app</span><span class="p">.</span><span class="nx">settings</span><span class="p">.</span><span class="nx">env</span> | ||
|
||
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> <html> <head> <title>conf.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="conf.html"> conf.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="module.html"> module.coffee </a> <a class="source" href="routes.html"> routes.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> conf.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p>Module requires</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">{</span><span class="nx">extend</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s1">'./helpers'</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <h2>Configurations</h2> | ||
|
||
<p>Configurations can be anything that you want to keep centralized, over here we are | ||
using some trival values as examples but well, for demonstration purposes. :) | ||
This can be helpful as you can define different values based on environment, | ||
such as database names, oAuth application details, or even something as simple as the | ||
title of the page.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">global = </span> | ||
<span class="nv">dir:</span> | ||
<span class="nv">views: </span><span class="s1">'/views'</span> | ||
<span class="nv">public: </span><span class="s1">'/public'</span> | ||
|
||
<span class="nv">development = </span> | ||
<span class="nv">sessionSecret: </span><span class="s1">'nyancat'</span> | ||
|
||
<span class="nv">production = </span> | ||
<span class="nv">sessionSecret: </span><span class="s1">'nyannyannyan'</span> | ||
</pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <h2>Export Module</h2> | ||
|
||
<p>Export module configurations based on environment</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">switch</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'../app'</span><span class="p">).</span><span class="nx">settings</span><span class="p">.</span><span class="nx">env</span> | ||
<span class="k">when</span> <span class="s1">'development'</span> <span class="k">then</span> <span class="nv">module.exports = </span><span class="nx">extend</span><span class="p">(</span><span class="nx">global</span><span class="p">,</span> <span class="nx">development</span><span class="p">)</span> | ||
<span class="k">when</span> <span class="s1">'production'</span> <span class="k">then</span> <span class="nv">module.exports = </span><span class="nx">extend</span><span class="p">(</span><span class="nx">global</span><span class="p">,</span> <span class="nx">production</span><span class="p">)</span> | ||
|
||
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<!DOCTYPE html> <html> <head> <title>module.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> module.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p>Example module file</p> </td> <td class="code"> <div class="highlight"><pre> | ||
<!DOCTYPE html> <html> <head> <title>module.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="conf.html"> conf.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="module.html"> module.coffee </a> <a class="source" href="routes.html"> routes.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> module.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p>Example module file</p> </td> <td class="code"> <div class="highlight"><pre> | ||
|
||
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html> |
Oops, something went wrong.