Skip to content

Commit

Permalink
added conf and helpers as examples
Browse files Browse the repository at this point in the history
  • Loading branch information
conancat committed Aug 6, 2011
1 parent 4b76876 commit f994e1f
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 31 deletions.
18 changes: 9 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
(function() {
var app, express;
var app, conf, express;
express = require("express");
app = module.exports = express.createServer();
conf = require('./lib/conf');
app.configure(function() {
app.set("views", __dirname + "/views");
app.set("views", __dirname + conf.dir.views);
app.set("view engine", "jade");
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
return app.use(express.static(__dirname + "/public"));
app.use(express.cookieParser());
app.use(express.session({
secret: conf.sessionSecret
}));
return app.use(express.static(__dirname + conf.dir.public));
});
app.configure("development", function() {
return app.use(express.errorHandler({
Expand All @@ -19,11 +23,7 @@
app.configure("production", function() {
return app.use(express.errorHandler());
});
app.get("/", function(req, res) {
return res.render("index", {
title: 'Express'
});
});
require('./lib/routes');
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
}).call(this);
35 changes: 23 additions & 12 deletions docs/app.html
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">&#182;</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">&quot;express&quot;</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">&#182;</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">&quot;express&quot;</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">&#182;</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">&#39;./lib/conf&#39;</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>App configurations</h2>

<span class="nx">app</span><span class="p">.</span><span class="nx">configure</span> <span class="o">-&gt;</span>
<span class="nx">app</span><span class="p">.</span><span class="nx">set</span> <span class="s2">&quot;views&quot;</span><span class="p">,</span> <span class="nx">__dirname</span> <span class="o">+</span> <span class="s2">&quot;/views&quot;</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">-&gt;</span>
<span class="nx">app</span><span class="p">.</span><span class="nx">set</span> <span class="s2">&quot;views&quot;</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">&quot;view engine&quot;</span><span class="p">,</span> <span class="s2">&quot;jade&quot;</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">&quot;/public&quot;</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">&#182;</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">&#182;</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">&quot;development&quot;</span><span class="p">,</span> <span class="o">-&gt;</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">&quot;development&quot;</span><span class="p">,</span> <span class="o">-&gt;</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">&quot;production&quot;</span><span class="p">,</span> <span class="o">-&gt;</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">&#182;</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">&quot;/&quot;</span><span class="p">,</span> <span class="nf">(req, res) -&gt;</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">render</span> <span class="s2">&quot;index&quot;</span>
<span class="nv">title: </span><span class="s1">&#39;Express&#39;</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">&#182;</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">&#182;</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">&#39;./lib/routes&#39;</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">&#182;</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">&quot;Express server listening on port %d in %s mode&quot;</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>
23 changes: 23 additions & 0 deletions docs/conf.html
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 &hellip; <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">&#182;</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">&#39;./helpers&#39;</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</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">&#39;/views&#39;</span>
<span class="nv">public: </span><span class="s1">&#39;/public&#39;</span>

<span class="nv">development = </span>
<span class="nv">sessionSecret: </span><span class="s1">&#39;nyancat&#39;</span>

<span class="nv">production = </span>
<span class="nv">sessionSecret: </span><span class="s1">&#39;nyannyannyan&#39;</span>
</pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</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">&#39;../app&#39;</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">&#39;development&#39;</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">&#39;production&#39;</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>
2 changes: 1 addition & 1 deletion docs/module.html
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">&#182;</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 &hellip; <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">&#182;</a> </div> <p>Example module file</p> </td> <td class="code"> <div class="highlight"><pre>

</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
Loading

0 comments on commit f994e1f

Please sign in to comment.