edit-task
Home
Up
Delete
Task Name:
Task Description:
5257 - express - MVC framework for node
TaskGroup ID:
Start Date:
Start Time:
Duration:
Priority:
Status:
To Do
Completed
In Process
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">node - expressjs</h1> <h2>[previously]</h2> <ol> <li><strong>[REFERENCE]</strong></li> <ol> <li>] # 5218 - learning <a href="/view/task?id=5218" target="_blank">node on windows</a> -</li> <li>] # 191 - <a href="/view/chapters?id=191" target="_blank">BOOK of javascript</a> </li> <li>] # # - <a href="http://expressjs.com/4x/api.html" target="_blank">API 4.x</a> </li> <li>] # # - <a href="http://stackoverflow.com/questions/tagged/express" target="_blank">stackoverflow</a>-express </li> <li>] # # - <a href="http://stackoverflow.com/questions/5778245/expressjs-how-to-structure-an-application" target="_blank">ExpressJS How to structure an application?</a> </li> </ol> <li><span style="background-color: #ffffff;"><strong>] NEW task IN </strong></span><ol> <li><span style="background-color: #ffffff;">] <strong>why express - </strong>personally, i fell into using/learning express, was included in a starter site template, then in a tutorial i looked at, it seemed as good as any i had used before(i hadnt used any before), and 100K+ cant be wrong can they?</span></li> </ol></li> <li><strong><span style="background-color: #ffffff;"><span style="background-color: #ffffff;"><span style="background-color: #ffffff;">] example - my first "express" app </span></span></span></strong><ol> <li><span style="background-color: #ffffff;">] <a href="/view/task?id=4800" target="_blank">PASS</a> - </span></li> <li><span style="background-color: #ffffff;">] using </span></li> <li><span style="background-color: #ffffff;">] PRB with calling out to an api and processing the results</span></li> <li><span style="background-color: #ffffff;">] </span></li> </ol></li> <li><strong>[00:00] NEW task IN CREATE-article# #</strong></li> <ol> <li>] # # - overview - a lightweight server side (MVC) application development framework, popular,</li> <ol> <li>PROs =</li> <li>CONS =</li> <li>ALTS = sails.js, loopback.io</li> <li>] express or sails - <a href="http://www.quora.com/Should-one-learn-Express-js-or-Sails-js" target="_blank">http://www.quora.com/Should-one-learn-Express-js-or-Sails-js</a> sails = express + ORM + MVC structure, ALSO loopback.io</li> </ol></ol> <li><strong>[00:00] NEW task IN </strong></li> <ol> <li>]</li> </ol> <li><strong>[00:00] how to - understanding how express works </strong></li> <ol> <li><strong>] app.js</strong> - your web server, the 'main' or your app</li> <ol> <li>] my example/sample program is named - server.js</li> <li>] this is the name of the file to launch when you start your app</li> <ol> <li>] node server.js</li> </ol> <li>] it is the main configuration file for your Express app.</li> <li>] .listen(port) -</li> </ol> <li><strong>] requires</strong></li> <ol> <li>] telling your app which program/packages and data to use The require function is pretty straight forward. <span style="background-color: #00ff00;">It's a built-in Node function that imports an object (<em>module.exports</em>) from another file or module.</span></li> <li><strong>] /routes</strong> - specifing a file which will define which routes your app will use</li> <li>] /users -</li> </ol> <li><strong>] requires --> module.exports()</strong></li> <ol> <li>] define code that will be avail, code file needs module.exports{ function(){} }, app need requires(''</li> </ol> <li><strong>] <a href="http://expressjs.com/api.html#app.set" target="_blank">app.set</a></strong></li> <ol> <li>]</li> <li>] app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'views'</span><span class="token punctuation">,</span> path<span class="token punctuation">.</span><span class="token function">join<span class="token punctuation">(</span></span>__dirname<span class="token punctuation">,</span> <span class="token string">'views'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> specify the path to the view folder</li> <li>] app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'view engine'</span><span class="token punctuation">,</span> <span class="token string">'jade'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> - specify which view engine to use</li> <li>*] CC jade templating package - simplifies your HTML files and gives you conditionals, Instead of saving your files as .html, you'll now have to save them as .jade in your /views folder</li> </ol> <li><strong>] <a href="http://expressjs.com/4x/api.html#app.use" target="_blank">app.use</a></strong> </li> <ol> <li>] tells the app how to use the parameters you're giving it. This can be a function or a path and a function</li> <li>] Mounts the <a href="http://expressjs.com/guide/using-middleware.html" target="_blank">middleware</a> <code>function</code>(s) at the <code>path</code>. If <code>path</code> is not specified, it defaults to “/”.</li> <li>] example defaults - app.use(bodyparser.json())</li> </ol> <li><strong>] special 404</strong></li> <ol> <li>] not the result of an error but rather the app running out of {route} options.</li> <li>] Once the request doesn't match any of the routes, it will reach the following function.</li> </ol> <li><strong>] error handling </strong></li> <ol> <li>] specify code in app.js to handle errors</li> <li>] render full errors in development mode VS error messages in production</li> </ol> <li><strong>] module.exports</strong></li> <ol> <li>] Remember that require function?(#2) That makes use of the module.exports! When you want to use some variables or functions from another file, you attach them to the module.exports.</li> <li><strong>] example -</strong></li> <ol> <li>var myModule = {</li> <li> add: function (a,b){</li> <li> return a+b;</li> <li> }</li> <li>module.exports(myModule);</li> </ol> <li><strong>] add a requires statement to your app.js</strong></li> <ol> <li><span class="token keyword">var</span> myModule <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'./mymodule.js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></li> </ol> <li><strong><span class="token punctuation">] use</span></strong></li> <ol> <li><span class="token punctuation">myModule.add(3,2); // 5</span></li> </ol> <li><strong><span class="token punctuation">] CC - practical experience</span></strong></li> <ol> <li><span class="token punctuation">] # <a href="/view/task?id=5273" target="_blank">5273 -v-004-002</a> - practical experience USING/implementing modules in the PASS app - SUMMARY = x] module works but diferent code than this document, ] unsuccessfull at making api call from within module, same code works in the 'index.js' page</span></li> </ol></ol></ol></ol> <h2>[currently]</h2> <ol> <li><strong><span style="background-color: #ffffff;">] adding requestjs </span></strong></li> <ol> <li><span style="background-color: #ffffff;">] for handling http requests</span></li> </ol></ol> <h2>[next]</h2> <ol> <li>] <a href="http://expressjs.com/starter/basic-routing.html">http://expressjs.com/starter/basic-routing.html</a> -</li> <ol> <li>]</li> </ol> <li>] <a href="http://jilles.me/getting-the-express-app-js/">http://jilles.me/getting-the-express-app-js/</a></li> <ol> <li>]</li> </ol> <li>] <a href="https://www.packtpub.com/books/content/understanding-express-routes" target="_blank">understanding express routes </a>-</li> <ol> <li>]</li> </ol> <li>] <a href="http://eloquentjavascript.net/04_data.html">http://eloquentjavascript.net/04_data.html</a> - data structures objects and arrarys</li> </ol> <p> </p> <hr /> <p> </p> <p>2] understanding expressjs</p> <p> </p> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #46830d;">// TRY adding special 404 - aka no routes matched</span> <span style="color: #4f76ac;">function</span> next<span style="color: #1e7c70;">(</span>err<span style="color: #1e7c70;">){</span> alert<span style="color: #1e7c70;">(</span><span style="color: #823125;">'doodledoop'</span><span style="color: #1e7c70;">);</span> <span style="color: #1e7c70;">}</span> app.use<span style="color: #1e7c70;">(</span><span style="color: #4f76ac;">function</span><span style="color: #1e7c70;">(</span>req<span style="color: #1e7c70;">,</span> res<span style="color: #1e7c70;">,</span> next<span style="color: #1e7c70;">)</span> <span style="color: #1e7c70;">{</span> <span style="color: #4f76ac;">var</span> err <span style="color: #1e7c70;">=</span> <span style="color: #4f76ac;">new</span> <span style="color: #4f76ac;">Error</span><span style="color: #1e7c70;">(</span><span style="color: #823125;">'that piglet is NOT Found'</span><span style="color: #1e7c70;">);</span> err.status <span style="color: #1e7c70;">=</span> <span style="color: #9b5f9b;">404</span><span style="color: #1e7c70;">;</span> res.status<span style="color: #1e7c70;">(</span>err.status <span style="color: #1e7c70;">||</span> <span style="color: #9b5f9b;">500</span><span style="color: #1e7c70;">);</span> res.render<span style="color: #1e7c70;">(</span><span style="color: #823125;">'./errors/404'</span><span style="color: #1e7c70;">,</span> <span style="color: #1e7c70;">{</span> message<span style="color: #1e7c70;">:</span> err.message<span style="color: #1e7c70;">,</span> error<span style="color: #1e7c70;">:</span> <span style="color: #1e7c70;">{}</span> <span style="color: #1e7c70;">})</span> <span style="color: #46830d;">//next(err);</span> <span style="color: #1e7c70;">});</span></pre>