UNPKG

2 kBMarkdownView Raw
1
2# Controllers
3
4Igo uses [Express 4](http://expressjs.com/) as a web framework for the Controller layer.
5
6
7## Routes
8
9Routes are defined in the `/app/routes.js` file.
10
11See the routing documentation here: See documentation here: http://expressjs.com/en/guide/routing.html
12
13```js
14module.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
25var compression = require('compression');
26app.use(compression());
27```
28
29### Static files
30
31All files located in `/public` are served statically with `express.static()`.
32
33```js
34app.use(express.static('public'));
35```
36
37### Cookies and Sessions
38
39Cookies are signed with the [cookie-parser](https://github.com/expressjs/cookie-parser) module.
40
41Sessions are encoded, signed and stored in cookies with [cookie-session](https://github.com/expressjs/cookie-session).
42
43It is highly recommended configure your own secret keys in `/app/config.js`.
44
45```js
46config.signedCookiesSecret = 'abcdefghijklmnopqrstuvwxyz';
47config.cookieSessionConfig = {
48 name: 'app',
49 keys: [ 'aaaaaaaaaaa' ]
50 maxAge: 24 * 60 * 60 * 1000 // 24 hours
51};
52```
53
54### Multipart data
55
56Multipart 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
60Request validation is made with [Validator.js](https://github.com/validatorjs/validator.js)
61
62### Flash Scope
63
64The Flash scope allows data to be stored in session during only 2 requests. Very useful when performing redirects.
65
66The Flash scope is a middleware as simple as this:
67
68```js
69module.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```