1 |
|
2 | # Controllers
|
3 |
|
4 | Igo uses [Express 4](http://expressjs.com/) as a web framework for the Controller layer.
|
5 |
|
6 |
|
7 | ## Routes
|
8 |
|
9 | Routes are defined in the `/app/routes.js` file.
|
10 |
|
11 | See the routing documentation here: See documentation here: http://expressjs.com/en/guide/routing.html
|
12 |
|
13 | ```js
|
14 | module.exports.init = function(app, controllers) {
|
15 | // welcome
|
16 | app.get ('/', controllers.WelcomeController.index);
|
17 | };
|
18 | ```
|
19 |
|
20 | ## Middlewares configuration
|
21 |
|
22 | ### Compression
|
23 |
|
24 | ```js
|
25 | var compression = require('compression');
|
26 | app.use(compression());
|
27 | ```
|
28 |
|
29 | ### Static files
|
30 |
|
31 | All files located in `/public` are served statically with `express.static()`.
|
32 |
|
33 | ```js
|
34 | app.use(express.static('public'));
|
35 | ```
|
36 |
|
37 | ### Cookies and Sessions
|
38 |
|
39 | Cookies are signed with the [cookie-parser](https://github.com/expressjs/cookie-parser) module.
|
40 |
|
41 | Sessions are encoded, signed and stored in cookies with [cookie-session](https://github.com/expressjs/cookie-session).
|
42 |
|
43 | It is highly recommended configure your own secret keys in `/app/config.js`.
|
44 |
|
45 | ```js
|
46 | config.signedCookiesSecret = 'abcdefghijklmnopqrstuvwxyz';
|
47 | config.cookieSessionConfig = {
|
48 | name: 'app',
|
49 | keys: [ 'aaaaaaaaaaa' ]
|
50 | maxAge: 24 * 60 * 60 * 1000 // 24 hours
|
51 | };
|
52 | ```
|
53 |
|
54 | ### Multipart data
|
55 |
|
56 | Multipart data is parsed with [multiparty](https://github.com/pillarjs/multiparty). The fields and the files are put in `req.body` and `req.files`, and ready to be used.
|
57 |
|
58 | ### Validation
|
59 |
|
60 | Request validation is made with [express-validator](https://github.com/ctavan/express-validator).
|
61 |
|
62 | ### Flash Scope
|
63 |
|
64 | The Flash scope allows data to be stored in session during only 2 requests. Very useful when performing redirects.
|
65 |
|
66 | The Flash scope is a middleware as simple as this:
|
67 |
|
68 | ```js
|
69 | module.exports = function(req, res, next) {
|
70 | req.session.flash = req.session.flash || {};
|
71 | res.locals.flash = req.session.flash;
|
72 | if (req.method === 'GET') {
|
73 | // clear flash scope
|
74 | req.session.flash = {};
|
75 | }
|
76 | req.flash = function(key, value) {
|
77 | req.session.flash[key] = value;
|
78 | };
|
79 | next();
|
80 | };
|
81 | ```
|