node - expressjs
[previously]
- [REFERENCE]
- ] # 5218 - learning node on windows -
- ] # 191 - BOOK of javascript
- ] # # - API 4.x
- ] # # - stackoverflow-express
- ] # # - ExpressJS How to structure an application?
- ] NEW task IN
- ] why express - 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?
- ] example - my first "express" app
- ] PASS -
- ] using
- ] PRB with calling out to an api and processing the results
- ]
- [00:00] NEW task IN CREATE-article# #
- ] # # - overview - a lightweight server side (MVC) application development framework, popular,
- PROs =
- CONS =
- ALTS = sails.js, loopback.io
- ] express or sails - http://www.quora.com/Should-one-learn-Express-js-or-Sails-js sails = express + ORM + MVC structure, ALSO loopback.io
- [00:00] NEW task IN
- ]
- [00:00] how to - understanding how express works
- ] app.js - your web server, the 'main' or your app
- ] my example/sample program is named - server.js
- ] this is the name of the file to launch when you start your app
- ] node server.js
- ] it is the main configuration file for your Express app.
- ] .listen(port) -
- ] requires
- ] telling your app which program/packages and data to use The require function is pretty straight forward. It's a built-in Node function that imports an object (module.exports) from another file or module.
- ] /routes - specifing a file which will define which routes your app will use
- ] /users -
- ] requires --> module.exports()
- ] define code that will be avail, code file needs module.exports{ function(){} }, app need requires(''
- ] app.set
- ]
- ] app.set('views', path.join(__dirname, 'views')); specify the path to the view folder
- ] app.set('view engine', 'jade'); - specify which view engine to use
- *] 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
- ] app.use
- ] tells the app how to use the parameters you're giving it. This can be a function or a path and a function
- ] Mounts the middleware
function(s) at the path. If path is not specified, it defaults to “/”.
- ] example defaults - app.use(bodyparser.json())
- ] special 404
- ] not the result of an error but rather the app running out of {route} options.
- ] Once the request doesn't match any of the routes, it will reach the following function.
- ] error handling
- ] specify code in app.js to handle errors
- ] render full errors in development mode VS error messages in production
- ] module.exports
- ] 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.
- ] example -
- var myModule = {
- add: function (a,b){
- return a+b;
- }
- module.exports(myModule);
- ] add a requires statement to your app.js
- var myModule = require('./mymodule.js');
- ] use
- myModule.add(3,2); // 5
- ] CC - practical experience
- ] # 5273 -v-004-002 - 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
[currently]
- ] adding requestjs
- ] for handling http requests
[next]
- ] http://expressjs.com/starter/basic-routing.html -
- ]
- ] http://jilles.me/getting-the-express-app-js/
- ]
- ] understanding express routes -
- ]
- ] http://eloquentjavascript.net/04_data.html - data structures objects and arrarys
2] understanding expressjs
// TRY adding special 404 - aka no routes matched
function next(err){
alert('doodledoop');
}
app.use(function(req, res, next) {
var err = new Error('that piglet is NOT Found');
err.status = 404;
res.status(err.status || 500);
res.render('./errors/404', {
message: err.message,
error: {}
})
//next(err);
});