-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (130 loc) · 3.28 KB
/
Copy pathindex.js
File metadata and controls
153 lines (130 loc) · 3.28 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Utility for returning html responses to browser.
*/
const CONST_CONTENT_TYPE = 'Content-Type'
const CONST_CONTENT_DISP = 'Content-Disposition'
const CONST_LOCATION = 'Location'
const CONST_TYPE_JSON = 'application/json; charset=utf-8'
const CONST_TYPE_XLS = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
const CONST_TYPE_SVG = 'image/svg+xml'
const CONST_TYPE_TEXT = 'text/plain'
const CONST_VARY = 'Vary'
const CONST_VARY_ACCEPT_ENCODING = 'Accept-Encoding'
function jsonErrorResponse(res, statusCode, messageKey, errKey, errorMessage) {
res.statusCode = statusCode
res.header(CONST_CONTENT_TYPE, CONST_TYPE_JSON)
const errorJsonResponse = { messageKey, errKey, errorMessage }
res.send(errorJsonResponse)
}
function notFound(res, message) {
res.statusCode = 404
if (message) {
res.send(message)
} else {
res.send('404 Page Not Found')
}
}
function error(res, err) {
res.statusCode = 500
if (err) {
res.send('500 Internal Server Error: ' + err)
} else {
res.send('500 Internal Server Error')
}
}
function json(res, data, statusCode) {
res.statusCode = statusCode || 200
res.header(CONST_CONTENT_TYPE, CONST_TYPE_JSON)
res.send(data)
}
function redirect(res, location) {
res.statusCode = 301
res.header(CONST_LOCATION, location)
res.end()
}
function unauthorized(res, data) {
res.statusCode = 401
res.send(data || 'unauthorized')
}
function forbidden(res, data) {
res.statusCode = 403
res.send(data || 'forbidden')
}
function jsonError(res, messageKey, errKey, errorMessage) {
jsonErrorResponse(res, 500, messageKey, errKey, errorMessage)
}
function excel(res, data, fileName) {
res.statusCode = 200
res.setHeader(CONST_CONTENT_DISP, 'attachment; filename="' + fileName + '"')
res.setHeader(CONST_CONTENT_TYPE, CONST_TYPE_XLS)
res.end(data, 'binary')
}
function text(res, data) {
res.statusCode = 200
res.setHeader(CONST_CONTENT_TYPE, CONST_TYPE_TEXT)
res.end(data)
}
function svg(res, data) {
res.statusCode = 200
res.setHeader(CONST_CONTENT_TYPE, CONST_TYPE_SVG)
res.setHeader(CONST_VARY, CONST_VARY_ACCEPT_ENCODING)
res.end(data)
}
module.exports = {
/**
* Status code 404. Page not found.
*/
notFound,
/**
* Status code 500, internal server error.
*/
error,
/**
* Status code 200 with content-type application/json
*/
json,
/**
* Status code 301 redirect, location provided as argument
*/
redirect,
/**
* Status code 401 with content-type application/json
*/
unauthorized,
/**
* Status code 403 with content-type application/json
*/
forbidden,
/**
* Status code 500 with content-type application/json
*
* messageKey and errKey should be keys that match
* a language translation construct
*/
jsonError,
/**
* Customizable status code with content-type application/json
*
* messageKey and errKey should be keys that match
* a language translation construct
*/
jsonErrorResponse,
/**
* Status code 200 with content-type application/vnd.openxmlformats
*
* Returns a binary excel file
*/
excel,
/**
* Status code 200 with content-type image/svg+xml
*
* Returns a svg file
*/
svg,
/**
* Status code 200 with content-type text/plain
*
* Returns a plain text string
*/
text,
}