-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
53 lines (45 loc) · 1.18 KB
/
middleware.js
File metadata and controls
53 lines (45 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var flatiron = require('flatiron')
, fs = require('fs')
, path = require('path')
, app = flatiron.app;
module.exports = function (req, res) {
res.error = {
json: function (err, code) {
res.json(code, { success: false, message: err.body || err.message });
},
html: function (err, code) {
res.html(code, fs.readFileSync(path.join(__dirname, 'public', 'error.html'), 'utf8'));
}
};
/**
* Redirect the user.
*/
res.redirect = function redirect() {
var args = arguments
, location
, code;
switch(args.length) {
case 1:
location = args[0];
code = 302;
break;
case 2:
location = +args[0] ? args[1] : args[0];
code = +args[0] ? +args[0] : args[1];
break;
default:
location = '/';
code = 302;
}
res.writeHead(code || 302, { 'Location': location });
if (!req.session) return res.end();
req.session.resetMaxAge();
req.session.save(function save() {
res.end();
});
};
// the originalURL is required as union does not work with the latest
// connect version.
req.originalUrl = req.originalUrl || req.url;
res.emit('next');
};