-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviewEngine.js
More file actions
55 lines (44 loc) Β· 1.57 KB
/
viewEngine.js
File metadata and controls
55 lines (44 loc) Β· 1.57 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
54
55
const ViewException = require('./viewException')
const ViewHelpers = require('./helpers')
const { ProxyHandler } = require('@ostro/support/macro')
class View {
constructor(app, http, next) {
let context = {};
Object.assign(context, (app['locals'] || {}));
Object.assign(context, global)
context.request = http.request;
context.session = http.session;
context.helpers = new ViewHelpers(app, http);
const fn = function(file, data = {}, status = 200) {
fn.render(...arguments)
return http.response
}
fn.engine = function(engine) {
Object.defineProperty(http.response, '__viewEngine', {
value: engine,
configurable: false,
writable: false,
enumerable: false
})
return this
}
fn.render = function(file, data = {}, status = 200) {
Object.assign(data, context)
app.view.engine(http.response.__viewEngine).renderFile(file, data, async (data) => {
Promise.resolve(data)
.then(res => {
if (typeof res == 'object' && res instanceof Promise == false) {
throw (new ViewException(res))
}
http.response.send(res, status)
})
.catch(e => {
next(e)
})
})
return http.response
}
return fn
}
}
module.exports = View